BINARY TOPIC 02 – NEGATIVE NUMBERS LESSON NOTE SIGNED
VS UNSIGNED REPRESENTATION A byte has 8 bits. With each bit
having 2 possible values, there are 28 = 256 different
combinations that can be stored in a byte.
Often, we can look at that as the numbers 0 up to 255. This approach is called unsigned
representation as it does not include negative values so there is no need to
consider the sign of the number. However, many applications require that we include the possibility of
using negative numbers. This is called
signed representation. In such
a situation, a byte might store values from -127 to 128 (instead of 0 to
256). There are several different ways to representing signed numbers in
binary. We will look at a few here: METHOD
1 - SIGNED MAGNITUDE
The range of a byte in such a setup is -127 to 127. One downside is that we get both 0 and -0
in this system. METHOD 2 – ONE'S COMPLEMENT In this strategy, we store a negative numbers by
starting off with its positive version and then taking the complement of the
byte – in other words, we invert every bit.
Again, we have the issue of having a +0 and a -0
with values 0000 0000 and 1111 1111.
The range for a byte in this setup is -127 to 127. METHOD
3 – TWO'S COMPLEMENT In this approach, we take the 1's complement of a
number and then add 1 to that value.
Essentially, adding one shifts over all negative values by one
eliminating the second zero.
METHOD
4 – OFFSET BINARY Example scenario: If -128 is set as 0000 0000, then -127 would be 0000
0001. And 0 would be 1000 0000. And 127 would be 1111 1111. METHOD
5 – BASE -2
Example - Convert 1011 0110-2 to base 10.
The answer is -128 + -32 + 16 + 4 + -2 = -142 MORE EXAMPLES Click here
to see all five methods above compared for 4-bit numbers. |