2.6 Singed and unsigned numbers

We are used to regular analog math where a negative number is designated by a negative symbol before the number. On the chance of over explaining things: a regular number in the base 10 system becomes negative when a ‘-’ is assigned to the number.

So, how are negative numbers represented in computers? The answer: a sign bit.

There exists several different methods of representing negative numbers but almost all of them use some form of a sign bit that indicates whether the number is positive or negative.

  • Numbers that only are positive or zero are called unsigned.
  • Numbers that can represent both positive and negative numbers are called signed.

It’s called signedness, where one bit in a binary number accounts for the negative part.

Numbers that are signed can represent both positive and negative numbers. There exists several different ways of representation, the most common representations are two’s complement and sign magnitude. Other existing methods that are not covered here but might be of interest to the curious reader are:

  • Ones’ complement
  • Offset binary
  • Base −2

No matter what representations is used, the addition of negative numbers requires extra information to be stored in the number itself, often in the form of a signed bit.


Sign magnitude representation

As discussed previously, a signed number uses a (sign) bit to represent whether the number is positive or negative. In the Sign magnitude representation the the leftmost bit simply assigns a positive or negative value to the rest of the number.

0 = positive, 1 = negative.

Consider the following signed numbers:

  • 00112 = 310
  • 10112 = -310

The leftmost bit determines if the number is positive or negative. But also consider how this bit defines the limitations of the number. An unsigned number with 8 bits can represent 28 numbers. A signed number with 8 bits can represent the same amount of numbers (28), but half of them are negative.

BitsMinMax
8 (signed)0255
8 (unsigned)-127127
16 (signed)−3276732767
16 (unsigned)065535

Range of Sign-Magnitude representation:

-2n-1 – 1 to 2n-1 – 1

One of the drawbacks with specifically the sign magnitude representation is the fact that there exists two zeros: 0 & -0.

  • 00002 = 010
  • 10002 = -010
  • 00012 = 110
  • 10012 = -110
  • 01112 = 710
  • 11112 = -710

Sign magnitude is a simple way to represent signed numbers, but it has its limitations, thus it does not see extensive use today.


Two’s complement representation

This limitation leads us to another representation widely used in modern computers; two’s complement. This implementation eliminates the duplicate zero (it only has one representation for zero). There are also benefits in terms of processing speed because of the simplified calculation of two’s complements signed numbers in actual hardware.

  • 00002 = 010
  • 00012 = 110
  • 00102 = 210
  • 01112 = 710
  • 10002 = -810
  • 10012 = -710
  • 10102 = -610
  • 11112 = -110
BitsMinMax
8 (signed)0255
8 (unsigned)-128127
16 (signed)−3276832767
16 (unsigned)065535

Range of two’s complement representation:

-2n-1 – 1 to 2n-1