Question

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?

Was it helpful?

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.

OTHER TIPS

The reason only the lowest 8 bits can be kept in the result is that the significance of each bit is constant: bit 0 is the ones place, bit 1 is the twos place, bit 2 is the fours place, etc. There is no way to "change" what any of these bits means without additional storage. Floating point, where you might (this is just a guess on my part) be getting your expectation that something else could happen has storage for an exponent, so that the significance of each bit in the significand can vary depending on the magnitude of the value you want to store. The important thing to realize is that this variation itself takes storage (in the form of the exponent) and cannot happen in an integer/fixed-point type where each bit has a particular fixed meaning.

The equivalent of this would be if you had 8 decimal digits with which to represent a number. In this case, we could represent 100,000,000 values, ranging from 0 to 99,999,999. As we counted up, we would have:

0
1
2
3

...

99,999,997
99,999,998
99,999,999
???

Since we don't have another digit to represent 100,000,000, what is the best course of action? We can't keep incrementing to 100,000,000 because that would require another digit we don't have. To make this work, we'd have to drop the 1 and get 00,000,000. This is still meaningful if we keep in mind that this number now represents 100,000,000 + whatever its value is. In this way, you can keep counting and the value remains at least somewhat meaningful:

99,999,998
99,999,999
00,000,000 represents 100,000,000
00,000,001 represents 100,000,001
00,000,002 represents 100,000,002
...

If we instead dropped the least significant digit (the 0 on the right), we would have:

99,999,998
99,999,999
10,000,000 ???
10,000,001 ???

There's no point in continuing to count because our number doesn't make sense anymore.

Once you remove the left-most bit, you have an 8-bit number and it can fit properly in a byte (unsigned char). It wouldn't make sense to remove more than one bit because you don't need to do that to reduce that particular number down to 8 bits. Only the lowest 8 bits can be kept when storing a value in an unsigned char.

C is a low level language, which maps closely to actual hardware. Underneath the type char, there is an assumption that the CPU has an 8-bit register, consisting of fixed amount of transistors and wires.

As the computer can't grow the number of physical resources, the type char is chosen to represent integers modulo 256.

On a computer architecture, where the primitive resource is an 9-bit unit, char holds 9 bits.

High level languages, which are often interpreted, OTOH can and do often "grow" the variable width.

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

It's really not a question of "only the left most bit being removed"...it's that the eight right-most bits are preserved. If not the eight right-most bits, then which bits do YOU think should be preserved? You can't keep all nine-bits, it's only an 8-bit value!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top