What is the binary representation of "256"? [closed]
Solution 1:
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.
Solution 2:
Well you need 2 bytes in order to represent that. 256 = 00000001 00000000