CIE AS & A Level Computer Science, section 1.1 (Data representation)
Binary addition
Add column by column from the right, with these rules:
| Sum | Result |
|---|---|
| 0 | |
| 1 | |
| 0, carry 1 | |
| 1, carry 1 |
Negative numbers
Computers store negative integers using two's complement.
- One's complement: invert every bit.
- Two's complement: invert every bit, then add 1.
In an 8-bit two's complement number, the leftmost bit has the value , so the range is to . A leading 1 means the number is negative.
Worked example 1: finding
. Invert: . Add 1: .
Check: ✓.
Binary subtraction
To calculate , add the two's complement of to and ignore any carry out of the leftmost bit.
Overflow
Overflow occurs when the result of an addition is too large to fit in the number of bits available.
(, which is above .) In 8-bit two's complement the answer reads as a negative number (), which is wrong. Adding two positive numbers and getting a negative result (or two negatives giving a positive) signals overflow.
Common mistake: forgetting the "add 1" step, so only finding the one's complement. Also, a carry out of the leftmost bit when subtracting is discarded; it does not mean overflow in itself.
Exam tip: show the invert and add-1 steps separately. State the number of bits being used, and check a negative answer by using the place values with on the left.
Check yourself
Find the 8-bit two’s complement of 20.
, invert to , add 1 to get (which is ).
What is the range of an 8-bit two’s complement integer?
to .
What is overflow?
The result of a calculation is too large to be stored in the available number of bits.