Frankie J
Frankie J

Reputation: 119

What exactly is a byte and what does it have to do with binary?

I'm just learning about binary and bytes. I understand that 8 bits make up a byte and that a byte can have 256 possibilities. The thing I am confused about is this:

byte[] b = new byte[] { 85, 85, 67, 75 };

What does 85 or any of the numbers above have to do with binary. There is simply something not fully clicking in my mind.

Upvotes: 8

Views: 1446

Answers (6)

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52157

85 is simply a decimal (i.e. "base 10") positional notation of a number. It means:

8*10^1 + 5*10^0

= 8*10 + 5*1

= 80 + 5

= 85

However, you can choose any base to represent numbers. Us humans have 10 fingers which is the probable origin of the "base 10" system in everyday use, but there is no way to represent 10 different digits in the physical circuitry of the computer. This circuitry only understands lack of electrons versus presence of electrons - i.e. only two digits, hence binary system.

So, while you can write decimal 85 in your program's source code (to keep it more natural to humans), it ultimately gets represented within the computer as binary 1010101, which equals...

1*2^6 + 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0

= 1*64 + 0*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1*1

= 64 + 16 + 4 + 1

= 85


BTW, decimal system does not "align" with binary - a single decimal digit cannot be represented by a "whole" number of binary digits and round decimal numbers do not match well the round binary numbers. For example:

  • 10 (decimal) = 1010 (binary)
  • 100 (decimal) = 1100100 (binary)
  • 1000 (decimal) = 1111101000 (binary)
  • etc...

Sometimes, it is convenient to use a numeric system that does "align" with binary, such as octal (one octal digit is exactly 3 binary digits) or hexadecimal (one hexadecimal digit is exactly 4 binary digits).

The important point is that all these systems eventually get represented as binary within the computer.

On a side-note: "hexa-decimal" used to be "sexa-decimal", but apparently offended some puritanic sensitivities ;)

Upvotes: 6

Christopher
Christopher

Reputation: 774

That statement declares an array of bytes, and assigns to it 4 elements - each of those being a single byte (in theory!) in size. The actual values - 85, 86, 67, 75 - stored in that array are each values that can fit into a single byte. In your code sample, those values are represented in decimal form. Once your code is translated into executable form, those values will eventually be seen by your computer in their binary representations, bandied about from register to register, pushed, popped, and accumulated, etc.

You could represent values in other bases within your source - hexadecimal, or base 16, for instance, which is closer to the metal than decimal, and correspondingly allows your thought process to move easily between human representation and what your code is actually saying since there's virtually no actual math to mentally go between hex and binary; another good example is octal.

Or you can think of it this way: represent values within your code using the base that best relates to what you're doing. If you're OR'ing together permissions or color values, hex or octal; if you're calculating interest on a checking account, then the obvious choice is decimal.

Now, for your specific code snippet - if you're dealing with individual characters or writing raw stuff to a block device, an array of bytes might be just fine - however, as you become one with your compiler, your brain may eventually consider hex the easier-to-handle representation.

Upvotes: 3

Bob Kaufman
Bob Kaufman

Reputation: 12835

Binary is how things are stored inside the computer. Ones and zeros, on and off, true and false, the presence or absence of an electric current. This Wikipedia article gives it a fairly thorough treatment.

A representation like "85" in your example is how a byte is formatted for our convenience as human readers. It could be formatted in hexadecimal (base 16) as "55", in octal (base 8) as "125" in binary as "1010101", or even "11" in base-84 if you were so inclined. All mean the same thing.

Upvotes: 12

Brett Walker
Brett Walker

Reputation: 3586

Each of the numbers in the array are less than 256. They are converted to a byte in binary. This is just their decimal representation.

Upvotes: 0

bdonlan
bdonlan

Reputation: 231461

Those are just numbers. They have nothing to do with binary per se. You can write them out in binary, though, if you so chose - eg, 75 = 01001011 in base 2.

A byte here, then, is just a data type which can hold one of 256 possible values. Typically this is interpreted either as a number from 0 to 255, or 8 binary digits (ie, 8 ON/OFF values). These are just two ways of looking at the same thing.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564921

A byte in C# is just an 8-bit integer value. In the above, you're representing 4 numbers in an array - this is really no different than it would be if you used int, except that the numbers take 1/4th of the total space in memory.

It actually has nothing to do with "binary" - other than it's the most common representation used for binary data. This is because most systems thing about one "byte" at a time, which is an 8 bit block of data. As such, a byte is a common way to represent this data if you're doing something that works with raw binary information.

The BitConverter class has routines for going from byte arrays to other types and back. For example, it can take 4 bytes and turn it into an Int32 value or go the other way around. Remember - all data stored is just memory - you're just saving a sequence of bits (1's and 0's) in a specific order, and the system is interpreting this data in a specific way.

Upvotes: 3

Related Questions