16

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?

3
  • 3
    As @Serious already said. You need one more bit, because computers start counting from 0. Thats an 11 in "hand language", or "one more person" (byte) in real world:)
    – SOMN
    Commented Aug 19, 2012 at 13:17
  • 2
    I closed this as "Not a real question" because it's difficult to tell what you actually want to know. Do you want to know the binary representation of a number? Are you wondering how it is possible that a number even exists even though it can't fit in a byte? Are you wondering about how data is stored in bits and bytes? Yes, 1 byte can't hold more than 8 bits, because that's the definition of a byte. But that does not impose any limits on numbers in general. Commented Aug 19, 2012 at 16:18
  • You cannot represent 256 on 8-bit.
    – Overmind
    Commented Jan 6, 2017 at 11:56

5 Answers 5

40

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.

Windows Calculator showing 256 in decimal and binary

12

Well you need 2 bytes in order to represent that. 256 = 00000001 00000000

11

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).

1
  • Yes, and the computer doesn't think of those 64 bits as a group of 8 separate bytes, but as a single unit consisting of 64 bits. For instance, if you have signed values then only one of those 64 bits is the sign bit.
    – Mr Lister
    Commented Aug 19, 2012 at 15:05
5

It is 100000000 and it needs more than one byte. Actually it needs 9 bits.

0

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.

Not the answer you're looking for? Browse other questions tagged .