Lesson 51: Binary adder
Add like a processor
- A processor adds binary numbers column by column, from the right, just like you add on paper.
- 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1), 1 + 1 + 1 = 11 (write 1, carry 1).
- An 8-bit register holds 0 to 255. A bigger answer does not fit: overflow.
Words for this lesson
| Word | Meaning |
|---|---|
| carry | the 1 that moves to the next column |
| register | a small store of bits in the processor |
| overflow | the answer needs more bits than the register has |
51 + 97 = 148
| Binary | Denary | |
|---|---|---|
| A | 0011 0011 | 51 |
| B | 0110 0001 | 97 |
| Sum | 1001 0100 | 148 |
- There are four carries, from the columns worth 1, 2, 32 and 64: in each of them two 1s add up to 10.
In the exam
- 0478/11/M/J/23 Q1(d): add 00110011 and 01100001 using binary addition. Mark scheme answer: 1001 0100, with the carries shown. (e) Why does an overflow error happen? Mark scheme: the result is greater than 255; it would need more than 8 bits, and the register has a fixed number of bits.
- 0478/13/M/J/23 Q1(d): add 11101110 and 00110001. Mark scheme: the 8 bits are 0001 1111, and one mark is for saying that an overflow error happened (287 needs 9 bits). Your second program prints the same 8 bits and lights the overflow LED.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Type two 8-bit binary numbers with a + between them, for example 00110011+01100001. Finish toNumber(s, start), which turns the 8 characters from start into a number: value = value * 2 + bit. The program adds the two numbers and prints Sum: , the 8 bits of the sum and the denary value in brackets.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The register has only 8 bits, so the biggest sum it can hold is 255. If the sum is more than 255, light the red LED on pin 13 and print Overflow! 8 bits: and the 8 bits that are left. If not, switch the LED off and print Sum: and the 8 bits.
اضغط تشغيل لرؤية المخرجات هنا.