Signed integers and binary arithmetic
Computers must store negative numbers as well as positive ones, and they must add and subtract them using nothing but bits. This note covers one's complement and two's complement representation, binary addition and subtraction, and overflow. Expect a Paper 1 question that asks you to convert a negative denary number to two's complement, add two binary numbers, and say whether the result is valid; the same ideas come back in the A Level floating-point topic.
Unsigned binary
An unsigned binary integer has no sign: every bit is worth a positive amount, so 8 bits store 0 to 255. Unsigned integers are fine for counts, addresses and pixel values, but cannot store . To store negative numbers, one bit pattern in every pair has to mean a negative value. There are several ways of choosing which; the syllabus asks for two.
One's complement
In one's complement, a positive number is written in ordinary binary with a leading 0. To make the negative of a number, invert every bit (change each 0 to 1 and each 1 to 0).
, so in one's complement .
The most significant bit (MSB) therefore acts as a sign bit: 0 for positive, 1 for negative. To read a negative one's complement number, invert it back and put a minus sign in front.
One's complement has a flaw: it has two zeros. 00000000 is and 11111111 (the inverse of zero) is . This wastes a pattern and makes arithmetic hardware more complicated, because the circuit has to treat two different patterns as equal. The 8-bit range is to .
Two's complement
Two's complement fixes both problems and is what almost every processor uses. The idea is simple: the most significant bit is given a negative place value.
| 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 |
.
Because only the MSB is negative, a number with MSB 0 is positive and a number with MSB 1 is negative. There is only one zero (00000000), and the range is lopsided by one: (10000000) to (01111111).
In -bit two's complement the MSB has place value and the range is
| Bits | Range |
|---|---|
| 4 | to |
| 8 | to |
| 16 | to |
In -bit one's complement the range is to , with two representations of zero.
Converting a negative denary number to two's complement
- Write the positive version of the number in binary using the full number of bits.
- Invert every bit (this is the one's complement).
- Add 1.
- Check by adding the place values with the MSB as .
A quicker equivalent: starting from the right of the positive number, copy every bit up to and including the first 1, then invert every bit to the left of it. For , copy 100, invert 00101 to 11010, giving 11010100 .
A third method uses the negative place value directly: for , start from and work out what has to be added to reach , which is , so the pattern is 1 followed by 1010011.
Converting two's complement to denary
If the MSB is 0, read it as ordinary binary. If the MSB is 1, either add the place values with the MSB worth , or invert and add one to find the magnitude and then write a minus sign.
Show as an 8-bit two's complement integer.
Solution
Invert: 11010010
Add 1: 11010011
Check: .
So .
The 8-bit pattern 10110110 is stored in a register. State its value as (a) an unsigned integer, (b) a two's complement integer, (c) a one's complement integer.
Solution
(a) Unsigned: .
(b) Two's complement: .
(c) One's complement: MSB is 1, so it is negative. Invert: 01001001 , so the value is .
The same bits mean three different numbers. A bit pattern has no meaning until you know how it is being interpreted.
Binary addition
Binary addition works column by column from the right, exactly as in denary, but you carry whenever a column reaches 2.
| Sum in a column | Write | Carry |
|---|---|---|
| 0 | 0 | |
| 1 | 0 | |
| 0 | 1 | |
| (with a carry in) | 1 | 1 |
Add the 8-bit binary numbers 00111001 and 00101110. Show your working and check in denary.
Solution
| Carry | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | |
| + | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| = | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |
The carry row shows the carry into each column, produced by the column to its right.
Result 01100111. Check: .
Binary subtraction
Processors do not usually have a separate subtraction circuit. Instead they compute as : find the two's complement of and add. This is one of the main reasons two's complement is used: one adder circuit does both jobs, and the sign takes care of itself.
- Write both numbers in the required number of bits.
- Find the two's complement of the number being subtracted.
- Add the two patterns.
- Discard any carry out of the most significant bit.
- Read the answer as a two's complement number and check for overflow.
Use 8-bit two's complement to calculate .
Solution
, so = invert 11000101, add 1 = 11000110.
| Carry | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | |
| + | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| = | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 |
Result 11011101. Value: . Check: .
Use 8-bit two's complement to calculate .
Solution
, so = invert 11110010, add 1 = 11110011.
01001101 + 11110011 = 1 01000000.
The ninth bit is a carry out of the MSB. In two's complement subtraction it is discarded, leaving 01000000 . Check: .
The carry out is not overflow here: a positive and a negative number were added, and the result is in range.
Overflow
Overflow happens when the result of a calculation is too large (or too negative) to fit in the number of bits available. The processor still produces a pattern, but it is wrong.
How you spot overflow depends on how the bits are being interpreted.
- Unsigned addition: overflow happens when there is a carry out of the MSB. For example, does not fit in 8 bits (maximum 255).
- Two's complement addition: overflow happens when two numbers of the same sign give a result of the opposite sign. Adding a positive and a negative number can never overflow.
Two's complement overflow rule:
- positive + positive giving a negative result: overflow;
- negative + negative giving a positive result: overflow;
- positive + negative: never overflows (a carry out of the MSB is simply discarded).
In the processor, overflow is recorded by setting the overflow flag in the status register; a carry out of the MSB sets the carry flag.
The 8-bit two's complement numbers 01011010 and 00110111 are added. Show the result and explain whether it is valid.
Solution
01011010 and 00110111 .
| Carry | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | |
| + | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 |
| = | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
Result 10010001. Its MSB is 1, so as two's complement it means .
Two positive numbers have produced a negative result, so overflow has occurred. The true answer, , is greater than , the largest 8-bit two's complement value. (As an unsigned number 10010001 is 145, which is correct, and there is no carry out: whether overflow has happened depends on the interpretation.)
Calculate in 8-bit two's complement and comment on the answer.
Solution
: , invert 10011011, add 1 = 10011100.
: , invert 11001101, add 1 = 11001110.
10011100 + 11001110 = 1 01101010. Discard the carry: 01101010 .
Two negative numbers have produced a positive result, so overflow has occurred. The true answer, , is below and cannot be represented in 8 bits. Using 16 bits would fix this.
Extending to more bits
To store an 8-bit two's complement number in 16 bits, copy the sign bit into all the new positions on the left. This is sign extension: becomes 1111111111010011, and becomes 0000000000101101. Padding a negative number with zeros would turn it into a large positive number.
Inverting without adding one. Inverting the bits gives one's complement, not two's complement. is 11010011 in two's complement; 11010010 is the one's complement.
Forgetting the number of bits. The two's complement of a number depends on the word length. Always write the positive number in the full width first: , not 101, before inverting.
Calling every carry out "overflow". In two's complement, a carry out of the MSB after adding a positive and a negative number is normal and is discarded. Use the sign rule to decide overflow.
Converting the answer as unsigned. After a two's complement calculation, an answer with MSB 1 is negative. Read it with the MSB as .
Typical marks for "show in two's complement" are one for the correct positive binary and one for the final pattern, so write both lines. For additions, show the carry row; examiners award the working even if one bit slips.
When asked to "explain" an overflow, say three things: the calculation exceeds the range of the representation, state that range (for example to ), and name the evidence (two positives gave a negative, or there was a carry out of the MSB for unsigned numbers).
If a question says "using two's complement" for a subtraction, you must convert the subtracted number and add; directly subtracting column by column, even if correct, may not earn the method mark.
- Unsigned: all bits positive; 8 bits give 0 to 255.
- One's complement: negate by inverting; two zeros; 8 bits give to .
- Two's complement: MSB worth ; one zero; 8 bits give to .
- Negate in two's complement by inverting all bits and adding 1.
- Subtract by adding the two's complement; discard any carry out of the MSB.
- Two's complement overflow: same-sign operands, opposite-sign result. Unsigned overflow: carry out of the MSB.
- Sign-extend by copying the MSB leftwards.
Practice
- Show and as 8-bit two's complement integers.
- Convert the 8-bit two's complement numbers
11100101and01111111to denary. - State the range of integers that can be represented in 12-bit two's complement.
- Write in 8-bit one's complement and in 8-bit two's complement.
- Add the unsigned 8-bit numbers
01101101and00110110, showing the carries. Give the answer in binary and in denary. - Use 8-bit two's complement to calculate . Show your working.
- Explain why two's complement is preferred to one's complement in processors. Give two reasons.
- Calculate using 8-bit two's complement. Explain what has happened and how a programmer could prevent it.
- The 8-bit registers A and B contain
11001000and10110000. They are added and the result is stored in an 8-bit register. (a) Interpreting both as two's complement, state the denary values and the stored result. (b) State whether overflow has occurred, justifying your answer. (c) Interpreting both as unsigned, state whether the stored result is correct. - A 4-bit two's complement system is used. List every bit pattern whose value is negative, with its denary value, and explain why there is one more negative value than positive value.
Answers
- , invert
11101011, add 1:11101100. , invert10011111, add 1:10100000. 11100101: .01111111: .- to , that is to .
- . One's complement =
10110010. Two's complement =10110011. - Carries (into each column, left to right)
11111000; result10100011= 163. Check . - =
11101100, =11110110. Sum =1 11100010; discard the carry:11100010. Correct, no overflow (two negatives, negative result). - Two's complement has only one representation of zero, so no pattern is wasted and no special zero test is needed; and subtraction can be done by addition with the same adder circuit, with the carry out simply discarded (one's complement needs an "end-around carry" to be added back). It also gives one extra value in the range.
- , ; sum
10100100. Two positives gave a negative: overflow, because . The programmer could use a larger data type (for example 16 bits) or check the overflow flag after the addition. - (a)
11001000,10110000; sum1 01111000, stored result01111000. (b) Yes: two negative numbers produced a positive result; the true answer is outside to . (c) Unsigned: ; there was a carry out of the MSB, so the stored result (120) is also wrong. 1000,1001,1010,1011,1100,1101,1110,1111. Half of the 16 patterns have MSB 1 (eight negatives); the other half have MSB 0, but one of those is zero, leaving only seven positives (1 to 7).