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

In this strategy, we simply reserve the leftmost bit to specify the sign of the number.  A zero would be "positive" and a one would be "negative".

Base 10

Signed Magnitude Binary

6

0000 0110

-6

1000 0110

120

0111 1000

-120

1111 1000

 

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.

 

Base 10

1's Complement

6

0000 0110

-6

1111 1001

120

0111 1000

-120

1000 0111

 

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

Most computer systems use this approach to store negative numbers.  It resolves the two zeros issue. 

 

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.

Base 10

2's Complement

6

0000 0110

-6

1111 1010

120

0111 1000

-120

1000 1000

 

METHOD 4 – OFFSET BINARY

Another method is called Offset binary or Excess-K.  It involves choosing a low value (such as -128) and making it be 0000 0000 and simply going up from there. 

 

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


Finally, one last method is called Base -2 (pronounced "base negative two").  This one is quite interesting in terms of calculations.  The base is negative.  So the value of the digits alternate from positive to negative (see chart below).  We can convert numbers to/from base 10 using the chart below in the example:

 

Example - Convert 1011 0110-2 to base 10.

 

1

0

1

1

0

1

1

0

(-2)7

(-2)6

(-2)5

(-2)4

(-2)3

(-2)2

(-2)1

(-2)0

-128

64

-32

16

-8

4

-2

1

 

The answer is -128 + -32 + 16 + 4 + -2 = -142

 

MORE EXAMPLES

 

Click here to see all five methods above compared for 4-bit numbers.