OCR H446, section 1.4.1 (c)
Unsigned binary can only store zero and positive numbers. To store negative integers, some of the bit patterns must stand for negative values. You need to know two methods.
Sign and magnitude
The leftmost bit is the sign bit: 0 means positive and 1 means negative. The remaining bits hold the magnitude (the size of the number) in ordinary binary.
In 8 bits:
| Sign | Magnitude (7 bits) | Value |
|---|---|---|
| 0 | 0000101 | |
| 1 | 0000101 |
So and .
Range
With bits, there are bits for the magnitude, so the values run from to . For 8 bits: to .
Problems
- There are two representations of zero: (+0) and (−0). This wastes a pattern and complicates comparisons.
- Arithmetic is awkward. Adding a positive and a negative needs the sign and size to be compared, and separate circuits for addition and subtraction.
Two's complement
In two's complement, the leftmost bit has a negative place value. In 8 bits:
| 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
So is in 8-bit two's complement.
- If the leftmost bit is 0, the number is positive (or zero), and is read as usual.
- If the leftmost bit is 1, the number is negative.
Range
With bits: from to .
| Bits | Smallest | Largest |
|---|---|---|
| 4 | ||
| 8 | ||
| 16 |
There is only one zero, and there is one more negative number than positive.
Why two's complement is used
- Only one zero.
- Addition and subtraction use the same circuit: to subtract, you add the negative (1.4.1 (d)).
- It makes the hardware simpler, so it is the standard in computers.
Converting a positive number to negative
Method: invert and add 1.
- Write the positive number in binary (in the full number of bits).
- Invert every bit (swap 0 and 1).
- Add 1.
Write in 8-bit two's complement.
- Invert:
- Add 1:
Check: . ✓
The same method turns a negative number back into a positive one: invert and add 1.
Shortcut: starting from the right, copy the bits up to and including the first 1, then invert all the bits to its left.
Converting two's complement to denary
Use the place values, with the leftmost bit worth negative .
Alternatively, invert and add 1 to get the magnitude: , so the value is .
Comparison
| Sign and magnitude | Two's complement | |
|---|---|---|
| How a negative is shown | Sign bit 1, then the magnitude | Negative place value on the left bit |
| in 8 bits | ||
| Range (8 bits) | to | to |
| Zeros | Two | One |
| Arithmetic | Needs extra rules | The same adder works for all |
| Used in | Rare today (some floating point formats use a sign bit) | Integers in almost all computers |
Worked example
Task: (a) write in 8-bit sign and magnitude and in two's complement. (b) What is in two's complement?
- (a) .
- Sign and magnitude: change the sign bit, giving .
- Two's complement: invert and add 1, giving . Check: . ✓
- (b) .
Exam tip: always state the number of bits. Show the invert, add 1 working, and check your answer by converting it back. For the range of two's complement, remember the extra negative value: to in 8 bits.
Check yourself
Write in 8-bit sign and magnitude
, so with the sign bit set it is .
Write in 8-bit two's complement
. Invert: . Add 1: .
What is the range of 6-bit two's complement numbers?
to , which is to .
Give one disadvantage of sign and magnitude
There are two representations of zero (+0 and −0), and arithmetic needs extra rules.