Binary arithmetic: adding, carrying and shifting
About this lesson
Computers add numbers the way you learned on paper, one column at a time, only with two digits instead of ten. Add 91 + 46 in binary, column by column, and watch each carry ripple to the left until the sum reads 10001001, which is 137. Then see what happens when a sum is too big for a byte, and why shifting bits left doubles a number.
Adding two bits
Add binary like decimal, one column at a time, but a column holds only 0 or 1. So 1 + 1 = 10: write 0 and carry 1 to the next column. And 1 + 1 + 1 = 11: write 1, carry 1.
>>> bin(0b1 + 0b1) '0b10' >>> bin(0b1 + 0b1 + 0b1) '0b11' >>> divmod(1 + 1, 2) # (carry, bit to write) (1, 0)
Read the lesson step by step
Adding two bits
Add binary like decimal, one column at a time, but a column holds only 0 or 1. So 1 + 1 = 10: write 0 and carry 1 to the next column. And 1 + 1 + 1 = 11: write 1, carry 1.
>>> bin(0b1 + 0b1) '0b10' >>> bin(0b1 + 0b1 + 0b1) '0b11' >>> divmod(1 + 1, 2) # (carry, bit to write) (1, 0)Line up the columns
Stack the numbers so the columns line up. a = 01011011 is 64 + 16 + 8 + 2 + 1 = 91, and b = 00101110 is 32 + 8 + 4 + 2 = 46. In Python, 0b marks binary.
>>> a = 0b01011011 >>> b = 0b00101110 >>> a, b (91, 46)Start at the right
Start in the ones column, at the right, just as on paper. Add the two bits: 1 + 0 = 1. The sum gets a 1 in its ones place and nothing carries.
>>> a = 0b01011011 >>> b = 0b00101110 >>> (a & 1) + (b & 1) # the two ones bits 1Your first carry
Next is the twos column: 1 + 1 = 2, which is 10 in binary. Write the 0 here and carry the 1 to the top of the column on its left, just like carrying a ten in decimal.
>>> bin(1 + 1) '0b10' >>> divmod(1 + 1, 2) # (carry, bit to write) (1, 0)The carry ripples left
Now each column adds three bits: the carry, a's bit and b's bit. The carry keeps moving left. In the eights column 1 + 1 + 1 = 3, which is 11: write 1 and carry 1.
>>> carry, out = 0, "" >>> for x, y in zip(reversed("01011011"), reversed("00101110")): ... carry, bit = divmod(int(x) + int(y) + carry, 2) ... out = str(bit) + out ... >>> out '10001001'The last column
In the 128s column only the carry is left: 1 + 0 + 0 = 1, and nothing carries out. The sum is 10001001. Every column used the same small rule, which is why a simple circuit can add.
>>> int("10001001", 2) 137Check in decimal
Check the answer in decimal. The sum's 1 bits are worth 128 + 8 + 1 = 137, and 91 + 46 = 137 too. Python agrees: bin(a + b) gives '0b10001001'.
>>> a, b = 0b01011011, 0b00101110 >>> a + b 137 >>> bin(a + b) '0b10001001' >>> 128 + 8 + 1 137Overflow: a byte is full
A byte has only 8 columns. Add 200 + 100 and the last column carries out a 1 with nowhere to go. That lost bit is worth 256, so the byte keeps 300 − 256 = 44. This is called overflow.
>>> 200 + 100 # Python ints never overflow 300 >>> (200 + 100) & 0xFF # keep only the low 8 bits 44 >>> (200 + 100) % 256 44Shift left to double
Moving every bit one place left doubles a number, because each place is worth twice the one to its right. 00101100 (44) becomes 01011000 (88). Python writes this 44 << 1. Shifting right halves it.
>>> 44 << 1 88 >>> bin(44), bin(88) ('0b101100', '0b1011000') >>> 88 >> 1 # shift right halves 44
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What is 1 + 1 in binary?
Binary has no digit 2. One plus one is two, which is written 10: a 0 in the ones place and a 1 carried into the twos place.
02In one column you add a carry of 1, plus 1, plus 1. What do you write, and what do you carry?
1 + 1 + 1 = 3, which is 11 in binary: write the right-hand 1 in this column and carry the left-hand 1 to the next column.
03What is 0101 + 0011 in binary?
From the right: 1 + 1 = 10 (write 0, carry 1); 1 + 0 + 1 = 10 (write 0, carry 1); 1 + 1 + 0 = 10 (write 0, carry 1); 1 + 0 + 0 = 1. So 1000. Check: 5 + 3 = 8.
04An 8-bit register adds 250 + 10. What value does it hold afterwards?
260 needs 9 bits (100000100). The 9th bit, worth 256, does not fit, so the byte keeps 260 − 256 = 4. In Python, (250 + 10) & 0xFF gives 4.
05What does 13 << 1 give in Python?
Shifting left by one place moves every bit up to a place worth twice as much: 1101 (13) becomes 11010 (26). So << 1 doubles the number.
Glossary
- Carry
- The 1 that moves into the next column when a column's sum is 2 or 3 (10 or 11 in binary), just like carrying a ten in decimal.
- Column (place)
- One position in a binary number. Each column is worth twice the one to its right: 1, 2, 4, 8, 16, 32, 64, 128.
- Full adder
- A small circuit that adds one column: three input bits (carry in, a, b) give one sum bit and one carry out.
- Ripple-carry adder
- Full adders chained side by side, so each column's carry ripples into the next column on its left.
- Overflow
- When a result needs more bits than the number can hold. In 8 bits, 200 + 100 = 300 does not fit, so the byte keeps 44.
- Byte
- A group of 8 bits. It can hold the whole numbers 0 to 255.
- Left shift (<<)
- Moving every bit one or more places to the left, filling with 0s. Each place left multiplies by 2.
- Mask (& 0xFF)
- Using bitwise AND with 0xFF (11111111) to keep only the low 8 bits of a number.
Notes and sources
- In hardware, the circuit for one column is called a full adder: three input bits (carry in, a, b) and two outputs (the sum bit and the carry out). Chain eight of them and the carries ripple from right to left, which is called a ripple-carry adder.
- Python's int has no fixed size, so 200 + 100 is simply 300. To see what an 8-bit register keeps, mask with & 0xFF (or take % 256). Languages with fixed-size integers, and the CPU itself, wrap around like this.
- Shifting left by n places multiplies by 2 to the power n (as long as no 1 bit falls off the left end). Shifting right by n divides by 2 to the power n and drops any remainder: 45 >> 1 is 22.
- Decimal works the same way: 7 + 5 = 12, so you write 2 and carry 1 ten. Binary just carries at 2 instead of at 10.
- A quick check for any binary sum: convert both numbers and the answer to decimal and see that the decimal sum matches.
- Harris & Harris, Digital Design and Computer Architecture, 2nd ed., sections 1.4.5 (binary addition) and 5.2.1 (adders)
- Bryant & O'Hallaron, Computer Systems: A Programmer's Perspective, 3rd ed., section 2.3 (integer arithmetic)
- MIT 6.004 Computation Structures notes: Basics of Information
- MIT 6.004 Computation Structures notes: Combinational Logic (adders)
- Python library: Bitwise operations on integer types (&, <<, >>)
- Python library: Built-in functions (bin, int)
Quiz Unlocks after the last step.