Adding 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.
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.
Column by column
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.
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.
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.
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.
Checks and limits
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 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.
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.
Try it in the Python prompt
;; step 1/9 · Adding two bits >>> bin(0b1 + 0b1) '0b10' >>> bin(0b1 + 0b1 + 0b1) '0b11' >>> divmod(1 + 1, 2) # (carry, bit to write) (1, 0)
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.