'Range of data type in cpp

Some where I have studied that in a signed data type one bit is used for sign so in that case positive value is 127 in char type as one can write + 1111111 in binary form so in negative also it should take value -127 as -1111111 but it is -128 why?

c++


Solution 1:[1]

Let's create a simple table, a list of all numbers that can be expressed in two, instead eight bits. One bit is still used for the sign, and one bit for the actual value. If we do that, then this is the table that we'll end up with:

decimal binary
-2 10
-1 11
0 00
1 01

So, that's what you get with two-bit long numbers. Note that the smallest value is -2 and the largest is 1.

Now, let's move on to three bits:

decimal binary
-4 100
-3 101
-2 110
-1 111
0 000
1 001
2 010
3 011

The smallest representable value is -4, and the largest one is 3.

If you continue on with this process, when you arrive at 8-bit numbers you will find that the smallest value will be -128 and largest one will be 127.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam Varshavchik