CS18: Binary Arithmetic
Binary addition rules, overflow errors, adding multiple binary numbers, and logical binary shift operations (left and right).
Binary addition rules, overflow errors, adding multiple binary numbers, and logical binary shift operations (left and right).
| Input A | Input B | Carry In | Sum | Carry Out |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
Carry: 0 1 1 0 0 0 0 0 Num A: 0 1 0 1 0 1 0 0 (84) Num B: 0 0 1 1 1 0 0 1 (57) ----------------------- Result: 1 0 0 0 1 1 0 1 (141)
Step through from right to left, carrying 1 whenever the column sum is 2 or more.
Carry: 0 1 1 0 1 1 0 0 Num A: 0 1 1 0 1 0 1 1 (107) Num B: 0 0 1 1 0 1 1 0 (54) ----------------------- Result: 1 0 1 0 0 0 0 1 (161)
Carry: 1 1 1 1 1 1 1 1 Num A: 1 1 1 1 0 0 0 0 (240) Num B: 0 0 0 0 1 1 1 1 (15) ----------------------- Result: 1 0 0 0 0 1 1 1 1 (BUT this is 9 bits!)
The result is 255, which needs 8 bits: 11111111. The 9th bit is a carry out that overflows.
Num A: 1 1 0 0 1 1 0 0 (204) Num B: 0 1 1 0 0 1 0 1 (101) ----------------------- Result: 1 0 0 1 1 0 0 0 1
The result needs 9 bits but we only have 8. The 9th bit (leftmost 1) is lost, leaving 00110001 = 49. The correct answer should be 305, but we get 49 - this is an overflow error!
Step 1: Add the first two numbers
01010011 (83) + 00110110 (54) --------- 10001001 (137)
Step 2: Add the result to the third number
10001001 (137) + 00011001 (25) --------- 10100010 (162)
Check: 83 + 54 + 25 = 162 โ No overflow.
Original: 0 0 0 1 0 1 1 0 (22) Shift left: 0 0 1 0 1 1 0 0 (44)
22 x 2 = 44. The shift left has multiplied the value by 2.
Original: 0 0 0 1 0 1 1 0 (22) Shift 1: 0 0 1 0 1 1 0 0 (44) Shift 2: 0 1 0 1 1 0 0 0 (88)
22 x 2^2 = 22 x 4 = 88. Each shift left multiplies by 2.
Original: 0 0 1 0 1 1 0 0 (44) Shift right: 0 0 0 1 0 1 1 0 (22)
44 / 2 = 22. The shift right has divided the value by 2.
Original: 0 0 0 1 0 1 1 1 (23) Shift right: 0 0 0 0 1 0 1 1 (11)
23 / 2 = 11.5, but integer division gives 11. The remainder (the rightmost 1 bit) is lost.
Original: 0 1 0 1 1 0 0 0 (88) Shift 1: 0 0 1 0 1 1 0 0 (44) Shift 2: 0 0 0 1 0 1 1 0 (22)
88 / 2^2 = 88 / 4 = 22. Each shift right divides by 2.
| Operation | Effect | Mathematical Equivalent | Bits Lost | Bits Added |
|---|---|---|---|---|
| Shift left by 1 | Move all bits left, fill right with 0 | Multiply by 2 | Leftmost bit | 0 on right |
| Shift left by n | Move all bits left n times | Multiply by 2^n | n leftmost bits | n zeros on right |
| Shift right by 1 | Move all bits right, fill left with 0 | Divide by 2 (integer) | Rightmost bit | 0 on left |
| Shift right by n | Move all bits right n times | Divide by 2^n (integer) | n rightmost bits | n zeros on left |
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Forgetting to carry in binary addition | 1+1 = 10, not just 0 | Always carry the 1 to the next column left |
| Not checking for overflow | The result may be incorrect if bits are lost | Check if there is a carry out from the leftmost bit |
| Shifting in the wrong direction | Left multiplies, right divides | Left = multiply, Right = divide |
| Forgetting that shift right loses data | The rightmost bit is discarded, not wrapped | Be aware that odd numbers lose their remainder |
| Shifting left causing overflow | If a 1 is shifted out, the value is wrong | Check if the result fits in the available bits |
Q1: Add the binary numbers 01011010 and 00110101. Show your working.
Q2: Add 10110011 and 01101010. Does overflow occur? Explain why.
Q3: Perform a binary shift left by 2 on 00101100. What is the result in decimal?
Q4: Perform a binary shift right by 1 on 01101010. What is the result in decimal?
Q5: What mathematical operation does a shift left by 3 perform?
Carry: 0 0 1 1 0 0 0 0 Num A: 0 1 0 1 1 0 1 0 (90) Num B: 0 0 1 1 0 1 0 1 (53) ----------------------- Result: 1 0 0 0 1 1 1 1 (143)
Num A: 1 0 1 1 0 0 1 1 (179) Num B: 0 1 1 0 1 0 1 0 (106)Result would be 100101101 (285), but this needs 9 bits. With only 8 bits, we get 00101101 (45). Overflow occurs because 179 + 106 = 285, which exceeds the maximum 8-bit value of 255. The carry out from the leftmost column indicates overflow.
โ Forgetting to carry when binary addition column sum exceeds 1 โ In binary: 1+1 = 10 (write 0, carry 1). 1+1+1 = 11 (write 1, carry 1). Always carry just like decimal addition.
โ Confusing binary shift direction โ left vs right โ Left shift multiplies by 2 (fills with 0 on right). Right shift divides by 2 and discards the remainder (fills with 0 on left for positive numbers).
โ Thinking overflow only occurs in very large numbers โ Overflow occurs whenever the result exceeds the available number of bits. Even small numbers like 11111111 + 1 = 100000000 overflow an 8-bit register.
โ Losing bits during a right binary shift and not recording them โ A right shift discards the least significant bit โ this is integer division by 2 with truncation. The lost bit represents the remainder and cannot be recovered.
Perform the binary addition: 10110110 + 01101001. Show your working including any carries, and state whether overflow has occurred in an 8-bit system. [4 marks]
10110110 + 01101001 -------- 100011111 Working right to left: 0+1=1, 1+0=1, 1+0=1, 0+1=1, 1+0=1, 1+1=0 carry 1, 0+1+1=0 carry 1, 1+0+1=0 carry 1 Result: 100011111 (9 bits) Overflow has occurred because the result requires 9 bits but an 8-bit register can only hold 8 bits. The 9th bit (carry out) is lost, and the stored result would be 00011111 = 31, which is incorrect.
AO1 (Computational Thinking โ 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including binary arithmetic: addition and shifts for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application โ 40%): Apply knowledge and understanding of computer science, including binary arithmetic: addition and shifts to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation โ 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including binary arithmetic: addition and shifts, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.