ENع
←Binary arithmetic
Lesson 02 · Numbers in computers

Binary arithmetic: adding, carrying and shifting

12 min9 steps5 questionsBeginner
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 2 · Interactive
01 / 09

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  2. 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)
  3. 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
    1
  4. Your 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)
  5. 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'
  6. 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)
    137
  7. Check 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
    137
  8. Overflow: 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
    44
  9. Shift 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

Quiz Unlocks after the last step.