Lesson 54: Negative numbers
Below zero
- 8 bits can store 0 to 255. To store negative numbers too, computers use two's complement.
- The leftmost bit is worth −128. All the other bits keep their normal values.
- So 8-bit two's complement stores −128 to +127.
Words for this lesson
| Word | Meaning |
|---|---|
| two's complement | binary for positive and negative whole numbers |
| most significant bit | the leftmost bit; 1 means negative |
| flip | change every 0 to 1 and every 1 to 0 |
Place values
| −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
- −128 + 32 + 2 + 1 = −93.
Make −32 (flip and add 1)
| Step | Bits |
|---|---|
| +32 | 0010 0000 |
| flip | 1101 1111 |
| add 1 | 1110 0000 |
In the exam
- 0478/11/M/J/24 Q3(c): convert 10100011 to denary. Mark scheme: −93, with working such as flip and add.
- 0478/12/M/J/24 Q3(d): −32 as a two's complement integer. Mark scheme: 11100000.
- 0478/13/O/N/24 Q1(e): 10001110 in denary. Mark scheme: −114.
- 0478/13/M/J/25 Q1(d): 11001001 in denary. Mark scheme: −55, method flip and add 1.
- 0478/13/O/N/23 Q2(d): the positive number 123 in 8-bit two's complement: 01111011, the same as normal binary.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
Type an 8-bit two's complement number, for example 10100011. The program already reads it as a normal binary number (163). In two's complement the leftmost bit is worth −128, not +128. So if the first bit is 1, take 256 away. Print Denary: and the value.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
Now make a negative number with the exam method. Type a positive number from 0 to 127, for example 32. The program prints it in 8 bits. Then flip every bit (0 becomes 1, 1 becomes 0) and print Flip: and the bits; then add 1 and print Add 1: and the bits. The result is −32 in two's complement.
Нажмите Запустить, чтобы увидеть результат здесь.