Search code examples
ccharunsignedbit

Why MSB is discarded in case of bits overflow?


Take unsigned char for example.

Its range is 0 to 255.

If I try to store 256 in it, following will happen :-

Binary of 256

100000000

As it consists of 9-bits hence MSB 1 will be discarded and it will become

00000000

Which is 0.

So I want to know that why only left most bit is removed?


Solution

  • Because it's the most reasonable & useful thing that could happen when you only have 8 bits of space.

    Discarding the lowermost bit would be a bad idea because now you can no longer increment the integer to get the next integer mod 28, etc... it would become useless as soon as it overflowed.
    Discarding any other bit in the middle would be bizarre, and any other kind of awkward transformation would effectively border on insanity.