Two´s complement works as follows;
A byte consists of 8 bits.
00000000 means 0
11111111 means 255
However, if the numbers were presented like that, we would not differentiate between whether the resulting number is positive or negative. Because of this reason, the bit on the left side gives us this information. If the bit on the left side is 0
, you can start adding the value of other bits on the top of zero
. If the bit is 1
, you should start adding on the top of -128
. Because the bit on the left side is two to the power of seven.
Examples;
In these examples, the bit on the left side is 1, it means we are adding the values of other bits on the top of -128.
10000000 = -128 (-128 + 0)
10000001 = -127 (-128 + 1)
10000011 = -125 (-128 + 3)
10000111 = -121 (-128 + 7)
Same bits but this time, the bit on the left is 0
. That means we are starting to add on the top of 0
.
00000000 = 0 (0 + 0)
00000001 = 1 (0 + 1)
00000011 = 3 (0 + 3)
00000111 = 7 (0 + 7)
If we are ok until now, the answer to your question,
the smallest possible number
10000000 = -128
the biggest possible number
011111111 = 127
That is why the range is between -128 and 127.