2.4 Representing numbers: negative integers
In Section 2.2. I showed you how integers can be encoded if they are known to be positive, treating the integers in the kitchen scales as
if they were known to be positive. However, if the user invokes the ‘add-and-weigh’ function on the scales while there is
an object in the scalepan and then removes the object, the display should record a negative value. Hence the computer in the
scales must be able to carry out subtractions and deal with resulting negative values. If the user is moving several things
on and off the scalepan, invoking the ‘add-and-weigh’ function on occasions, then the computer must be able to determine at
all times what value it is to display and whether this value is positive or negative. The computer must, therefore, be able
(a) to do arithmetic, (b) to handle negative values and (c) to distinguish between positive and negative values.
In this section I shall show you how negative numbers can be encoded and distinguished from positive numbers. The aspect of
arithmetic will be dealt with later, in Section 7.
An integer which may be positive or negative is known as a signed integer. In any code system for signed integers, it is important to indicate whether the integer is positive or negative.
With ordinary denary numbers, a signed positive integer is prefixed by a plus sign and a signed negative integer is prefixed
by a minus sign. Each signed integer has an additive inverse which is obtained by replacing the plus sign by a minus sign, or vice versa. For example,+ 5 has the additive inverse − 5;
− 20 has the additive inverse+ 20, and so on. A number and its additive inverse have the property that zero is obtained when
they are added together. This system of representation is called the sign-and-magnitude system.
You will probably be very familiar with the sign-and-magnitude system, and it is tempting to try to find a way of using it
for binary numbers, perhaps by making the most-significant bit into a ‘sign bit’ and saying that 0000 0101 is + 5 while 1000 0101
is − 5. Unfortunately, however, this coding system would make arithmetic with binary numbers awkward for computers and so
it is not normally used.
The coding system that is used adapts the positional notation I introduced in Section 2.2 to allow for negative numbers. Specifically, the most-significant bit is given a negative weighting. With 8-bit codes, the weightings are then as follows:
The advantage of this system is that it makes the addition and subtraction of signed integers more straightforward for the
computer's processor, as you'll see in Section 7.
Example 2
Convert the signed binary integer 1101 0110 to denary.
Answer
Using the weightings for 8-bit signed integers, 1101 0110 is equivalent to:
| −27
|
26
|
25
|
24
|
23
|
22
|
21
|
20
|
| (−128) |
(64) |
(32) |
(16) |
(8) |
(4) |
(2) |
(1) |
| 1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
which is −128 + 64 + 16 + 4 + 2 = −42 in denary.
In this coding system, the leftmost bit (bit 7) is 0 for a positive number and 1 for a negative number, so at first glance
you might think of it simply as a ‘sign bit’. But this leftmost bit does more than simply acting as a ‘sign bit’: it contributes
−128 to the number whenever its value is 1, thereby forcing the number to be negative whenever it is 1.
This system of encoding is called the 2's complement system and the resulting codes are often referred to as 2's complement numbers.
The process of converting a positive denary 8-bit number to its 2's complement equivalent is almost identical to the process of converting a denary number to
an ordinary binary number. As it is positive, it cannot have a minus 128 in it, and so the leftmost bit is 0. The conversion process then starts by comparing the largest positive power of 2 with the given number.
If a negative denary number is to be converted, however, it must first be expressed as the sum of a negative and a positive number, as
the following example shows.
Example 3
Convert the denary number −122 to an 8-bit 2's complement number.
Answer
The sign bit of an 8-bit 2's complement number has weighting −128. Therefore −122 must be expressed as:
−128 + (a positive number)
Clearly here the appropriate positive number is 6, so −122 is −128 + 6.
Because the number contains −128, the leftmost bit of the binary representation is 1. The other 7 bits are the 7-bit binary
equivalent of 6, which is 000 0110. So the 8-bit 2's complement equivalent of −122 is:
1000 0110.
The rule is first to add 128 to the negative number, then to convert the result to a 7-bit binary number. The 8-bit number
is formed by prefixing the 7-bit number by 1 (because there is one −128 in the given negative number).
Note that −1 in denary is −128 + 127 and so is equal to 1111 1111 in 2's complement representation.
Activity 12 (Self assessment)
-
What denary number is equivalent to each of the following 2's complement numbers:
-
1011 0111
-
0101 1011?
-
What is the 8-bit 2's complement equivalent of the denary numbers
-
25
-
−96
-
−2?
Now read the answer
(a) (i) The number is equivalent to
which is −128 + 32 + 16 + 4 + 2 + 1 = −73 in denary.
(ii) The number is equivalent to
which is 64 + 16 + 8 + 2 + 1 = 91 in denary.
(b)(i) 25 is positive, so the leftmost bit is zero. The remaining 7 bits are used to represent 25, which is 001 1001 in 7-bit
binary. The 8-bit 2's complement number is therefore 0001 1001.
(ii) −96 must be expressed as −128 + (a positive number); that is, as −128 + 32. The leftmost bit is 1 (representing the −128)
and the remaining 7 bits are equivalent to 32, which is 010 0000. The 8-bit 2's complement number is therefore 1010 0000.
(iii) −2 is −128 + 126. The leftmost bit is 1 (representing he −128) and the remaining 7 bits are the 7-bit binary equivalent
of 126, which is 111 1110. The 8-bit 2's complement number is therefore 1111 1110.
In the 2's complement system, if an 8-bit word is used, the largest positive number that can be represented is 0111 1111,
which is + 127, and the largest negative number that can be represented is 1000 0000, which is −128. In total, 127 positive
numbers, zero and 128 negative numbers – that is, 256 different numbers – can be represented, which is to be expected from
eight bits.
If more than eight bits are used then a greater range of numbers can be represented. For instance, a 16-bit word can represent
signed integers in the range −32 768 to + 32 767.
In the case of a 16-bit number the leftmost bit, in this case bit 15, again acts as a sign bit. The weighting of this bit
is −215 and the weightings of the remaining fifteen bits are 214, 213, …, 20.
Activity 13 (Exploratory)
You saw in Section 2.2 that 12 bits would be needed to encode the positive values 0 to 3000 if the kitchen scales worked with only positive integer
numbers of grams. But of course they work with both positive and negative integers, and so the computer must be able to cope
with this. What is the smallest number of bits that is needed to encode the signed integers −3000 to +3000?
Now read the discussion
Comment
Did you spot that the number of bits is simply one more than the 12 needed for positive integers because twice as many values
now need to be represented? So 13 bits would be sufficient. In practice, of course, the computer will simply use two 8-bit
words.
|