You can convert decimal to binary and binary to decimal. But how can you represent the number "256"?
255 = 11111111
1 byte cannot have more than 8 binary digits (bits). How is this possible?
You can convert decimal to binary and binary to decimal. But how can you represent the number "256"?
255 = 11111111
1 byte cannot have more than 8 binary digits (bits). How is this possible?
You ask for how to represent 256 in binary, but I'm guessing you're wondering why people say bytes can store 256 different numbers, when the largest number it stores is 255. As Claudiop said, computers start counting at 0, so 0 is actually the first number, 1 is the second, 2 is the third... 255 is the 256th.
Also, 11111111 is only 255 for unsigned bytes. When you have a signed byte (a signed value is one that can hold negative values), 11111111 is actually -1. See http://en.wikipedia.org/wiki/Two's_complement. The way two's complement works, adding a negative number to a positive number results in 0. As other people have said, if we add a bit to 11111111, and your datatype can only support 8 bits, the last bit will overflow and leave you with 0. For signed bytes, the values range from -128 to 127. 128 negative numbers + 0 + 127 positive numbers = 256 numbers total.
For signed values, the first bit is the "sign" bit. If this bit is set, the number is negative. 10000000 is negative, 01000000 is positive, 11111111 is negative, 01111111 is positive...
If you're on windows (maybe mac has it too), you can open up the calculator, switch it to programmer mode, choose sbyte, and play around with the bits to see how they correlate to their decimal representations.
As you already know, 255d (decimal) equals 11111111b (binary). If you now want to add 1 to the value, there are two possibilities:
Either you only have 8 bits. In this case, a so-called overflow happens. So "internally", the 1 will be added resulting in 100000000b (256d in 9 bits). But because you only have 8 bits available, the lower 8 bits will be "returned". So you end up with 0d = 0b (a special overflow flag will be set on most computer architectures, just as a side note.)
Think of this the same as counting with your fingers. Imagine your fingers show 9d. Now you add one more finger. You end up with 10. What do you do if you want to add even one more?
The other possibility is that you have more than 8 bits available. In this case you can simply add one more digit to the beginning and the result will really be 100000000b = 256d.
A byte is the smallest "unit" a computer system (memory system) can address. This means that if you want to know only a single bit, you have to ask the memory system to give you a certain byte from an address and you then have to figure out the value of the bit your are interested in yourself.
But the same way as 8 bits make up a byte, there are larger datatypes as well. 2 bytes make up a word (16 bits), two words (four bytes, 32 bits) make up a double word. And the nowadays standard 64 bit architectures even have 64 bit datatypes (so called registers).
You can represent it by a bit shift operation (left or right depending on the endianness of binary representation). For example this one liner works for a big endian representation (most significant byte first):
1 << 8
An unsigned byte can only hold 256 values which includes the range of [0 - 255]. For the value 256 you would need to use a data type that is able hold a larger value, for example an integer.