Binary addition, overflow, shifts and two's complement
Computers add binary numbers in columns, just as you add denary. You add two 8-bit numbers and see when the answer is too big for the register. Then you shift bits to multiply and divide, and store negative numbers with two's complement.
about 30 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
Adding in binary
Binary addition works like denary addition in columns, from right to left. The only difference is that you carry when a column reaches 2, not 10. These four rules are all you need.
0+0=0.
0+1=1.
1+1=10: write 0 and carry 1.
1+1+1=11: write 1 and carry 1.
A binary column addition. The top row is 00110101 and the second row is 01011110, with the place values 128 to 1 above the columns. Small carry digits of 1 are written above the columns that receive a carry. The answer row is 10010011. Beside it the denary check 53 + 94 = 147 is written.
The picture adds 00110101 (53) and 01011110 (94). Follow the carries from the right.
Columns 1 and 2 (from the right):1+0=1, then 0+1=1.
The answer is 10010011. Check in denary: 53+94=147, and 128+16+2+1=147.
Overflow
A register has a fixed number of bits. An 8-bit register can hold 0 to 255, and nothing bigger. Every computer or device has a limit like this, for example 16 bits.
If the answer to a sum is greater than 255, it needs a ninth bit. There is no ninth place in an 8-bit register, so that carry is lost. This is an overflow error. It happens whenever a value outside the limit should be returned.
Add 11001000 (200) and 01100100 (100). The true answer is 300, which needs 9 bits: 100101100. The register keeps only the right-hand 8 bits, 00101100, which is 44. The answer is wrong, and the processor flags an overflow.
Logical shifts
A logical shift moves every bit of the register the same number of places to the left or to the right.
Bits that are shifted off the end of the register are lost.
Zeros are shifted in at the opposite end.
Two 8-bit registers, one above the other. The top register holds 00101101. Arrows show every bit moving two places to the left. The two bits on the left, both 0, fall off the end and are marked lost, and two 0s enter on the right. The lower register holds 10110100. The labels say 45 before the shift and 180 after it.
Left shift. Each place to the left doubles the value. A shift of 2 places multiplies by 4, and 3 places by 8. In the picture, 00101101 (45) shifted 2 places left becomes 10110100 (180). That is 45×4.
Right shift. Each place to the right halves the value. A shift of 2 places divides by 4. 10110100 (180) shifted 2 places right is 00101101 (45) again.
When a 1 is lost. If a left shift pushes a 1 off the left end, the most significant bits are lost and the answer is no longer the true product. If a right shift pushes a 1 off the right end, the least significant bits are lost and the answer is rounded down. For example 00001011 (11) shifted 1 place right is 00000101 (5), not 5.5.
Two's complement: negative numbers
So far every number has been positive. To store negative numbers, the two's complement system gives the most significant bit a negative place value. In 8 bits the place values are these.
−128, 64, 32, 16, 8, 4, 2, 1
An 8-bit two's complement register. Above the boxes the place values are minus 128, 64, 32, 16, 8, 4, 2 and 1, with the minus 128 in red. The boxes hold 1, 0, 1, 1, 0, 1, 1, 0. Under the row the sum minus 128 + 32 + 16 + 4 + 2 = minus 74 is written.
An MSB of 0 means the number is positive or zero, and the other bits are read as usual. An MSB of 1 means the number is negative. The range of 8-bit two's complement is −128 to +127.
Two's complement to denary. Add the place values under the 1s, using −128 for the MSB. For 10110110: −128+32+16+4+2=−74.
A positive number to two's complement. Write it in binary as usual, with an MSB of 0. So +74 is 01001010.
A negative number to two's complement. There are two methods.
Flip and add 1. Write the positive number in 8 bits. Change every 0 to 1 and every 1 to 0. Then add 1. For −74: 01001010 flips to 10110101, and adding 1 gives 10110110.
Start from −128. Work out how much you must add to −128. For −74 you need −128+54, and 54=32+16+4+2. So the answer is 10110110 again.
Check with the place values: −128+32+16+4+2=−74.
Practise with new numbers
Try it in Python
The program adds two 8-bit numbers column by column with carries. It reports an overflow if a carry comes out of the last column. Then it turns −74 into two's complement by flipping the bits and adding 1.
Common mistakes
Losing a carry. Write each carry above the next column before you add it.
Saying "the answer is 9 bits". In an 8-bit register the ninth bit is lost. That is the overflow.
Shifting the wrong way. A left shift multiplies and a right shift divides.
Forgetting to add 1. Flipping the bits alone gives the wrong number. Flip, then add 1.
Reading a negative number as positive. In two's complement an MSB of 1 is worth −128.
Remember these
Carry when a column reaches 2. 1+1=10 and 1+1+1=11.
An overflow happens when the answer is greater than 255 in an 8-bit register.
A logical left shift of k places multiplies by 2k. A right shift divides by 2k. Bits shifted off the end are lost and zeros come in.
In 8-bit two's complement the MSB is worth −128, and the range is −128 to +127.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Add the 8-bit binary numbers 00011011 and 00000110.
Does adding 10000000 and 10000000 in an 8-bit register cause an overflow?
The register holds 00010110. What does it hold after a logical left shift of 3 places?
Write the denary number −20 as an 8-bit two's complement number.
What denary number does the two's complement pattern 11111110 stand for?
Enroll free in Binary, Denary and Hexadecimal
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.