الأسبوع 5 · الوحدة ٦ «تصميم المعلومات» (٢): العمليات الحسابية والمكمّلات، رقمنة الصوت والصور والفيديو والضغط، تصميم المعلومات · 3 دروس
1
Calculating in binary: adding, shifting, negatives
ICT Secondary 1 Term 1, Chapter 6, Lessons 6-5 to 6-10: pp. 65–84
GoalAdd binary numbers with carries, shift bits, and write a negative number with two’s complement.
New words
carry · the 1 passed to the next column when a column adds to 2 or moreshift · moving all bits one place left or righttwo’s complement · flip all bits, then add 1: the way to write a negative numberoverflow · a result that needs more bits than are available
Explanation
Binary addition works like decimal addition, column by column, from the right. The rules are 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10 (write 0, carry 1). When a column has a carry as well, 1 + 1 + 1 = 11 (write 1, carry 1). Shifting all bits one place to the left doubles a number: 0000 0101 (5) becomes 0000 1010 (10). Shifting right halves it, dropping the last bit: 0000 0111 (7) becomes 0000 0011 (3). To write a negative number in 8 bits, find the one’s complement (flip every bit) and add 1; this is the two’s complement. For −5: 0000 0101 flips to 1111 1010, plus 1 gives 1111 1011. Check by adding the number and its negative: 0000 0101 + 1111 1011 = 1 0000 0000, and the ninth bit does not fit in 8 bits, so the result is 0000 0000. With 8 bits this way we can write −128 to +127. Overflow happens when a result needs more bits than there are: in 4 bits, 1011 + 0110 = 10001, but only 0001 fits. The same idea works in decimal: with one digit, the 10’s complement of 3 is 10 − 3 = 7, and 3 + 7 = 10 drops its carry.
Adding 0110 1011 (107) and 0010 0110 (38)
Bits, left to right
carry into the column
1 1 0 1 1 1 0 0
first number
0 1 1 0 1 0 1 1
second number
0 0 1 0 0 1 1 0
sum
1 0 0 1 0 0 0 1
Worked examples
Column step
4 bits
first number
1 0 1 1
second number
0 1 1 0
true sum (5 bits)
1 0 0 0 1
what 4 bits keep
0 0 0 1
The sum 1001 0001 is 128 + 16 + 1 = 145, and 107 + 38 = 145, so the addition is right. Now overflow: in a 4-bit register, 1011 (11) + 0110 (6) = 17, which needs 5 bits (10001). The register keeps only 0001, so the answer is wrong. The cure is more bits.
Step
Bits
5
0000 0101
flip every bit
1111 1010
add 1: this is −5
1111 1011
check: 5 + (−5), 8 bits
0000 0000
Two’s complement of 5 in 8 bits, step by step, and the check.
Class activity
Write 7 as 8 bits. Find its two’s complement to get −7, and check that 7 + (−7) gives 0000 0000 in 8 bits. Then shift 7 one place to the left and say what number you get.
Practice
1
Add 0110 and 0011.
1001 (6 + 3 = 9).
2
What does shifting 0000 0100 one place to the left give?