Binary Addition

You already know how to add numbers on paper: stack them up, add the right-hand column first, and carry a digit to the next column whenever a total gets too big to fit. Adding binary numbers works in exactly the same way — line the bits up, add from the right, and carry. The only difference is how soon you have to carry. In ordinary (denary) numbers a column can hold up to 9 before it overflows; in binary the biggest digit is 1, so a column overflows the moment it reaches 2.

This matters because addition is the one operation a computer's processor does more than any other. Multiplication is just repeated addition, subtraction is addition of a negative, and even drawing graphics or running a game boils down to a chip called the ALU adding binary numbers billions of times a second. Learn these four little rules and you know how a computer does sums.

The four rules

Because each bit is only 0 or 1, there are just four things that can happen when you add a single column (once you include a carry that arrived from the column on its right). Memorise these and you can add any two binary numbers:

Notice the pattern: you write down the ones part of the total and carry the twos part — precisely what you do in denary, where you write the units and carry the tens. Binary is not a new skill, just the same skill with an earlier carry.

A worked column addition

Let's add 0110 (that's 6) and 0101 (that's 5). The answer should come out as 11. Step through the columns from right to left — watch the little carry row along the top fill in as we go:

Reading the bottom row: 1011, which is 8 + 2 + 1 = 11. Exactly right — and every carry we made was just the 1 + 1 = 10 rule in action.

Add binary strings yourself — edit and Run

This program adds two binary numbers the way you would on paper: it walks both strings from the right, applies the four rules with a running carry, and builds the answer digit by digit. At the end it double-checks the result by converting everything to ordinary numbers with parseInt. Change the two a and b strings and press Run.

// Two binary numbers to add (use only 0s and 1s): const a: string = "0110"; // 6 const b: string = "0101"; // 5 let i: number = a.length - 1; let j: number = b.length - 1; let carry: number = 0; let result: string = ""; while (i >= 0 || j >= 0 || carry > 0) { const bitA: number = i >= 0 ? Number(a[i]) : 0; const bitB: number = j >= 0 ? Number(b[j]) : 0; const sum: number = bitA + bitB + carry; // 0, 1, 2 or 3 result = (sum % 2) + result; // write the "ones" part carry = Math.floor(sum / 2); // carry the "twos" part i = i - 1; j = j - 1; } console.log(a + " + " + b + " = " + result); // Check it by converting to ordinary numbers: const check: number = parseInt(a, 2) + parseInt(b, 2); console.log("Check: " + parseInt(a, 2) + " + " + parseInt(b, 2) + " = " + check + " = binary " + check.toString(2));

The loop keeps going while there are bits left in either number or a carry still needs writing — that last condition is what lets the answer grow an extra digit when two big numbers overflow their width.

When a carry runs off the end

On paper the answer can always grow one more column on the left. Inside a computer it often can't: numbers are stored in a fixed number of bits — commonly 8 (a byte). If the true answer needs a ninth bit, there is nowhere to put the final carry, so it is simply thrown away and the stored result is wrong. This is called overflow.

Add the two 8-bit numbers 11111111 (255) and 00000001 (1). The true answer is 100000000 (256) — but that needs 9 bits. With only 8 to spare, the leading 1 carries off the end and vanishes, leaving 00000000: the computer thinks 255 + 1 = 0!

Processors keep a special carry flag that switches on exactly when this happens, so a program can notice and react. But if nobody checks it, overflow is silent — and it is behind real bugs, from the arcade game Pac-Man's unplayable level 256 to aircraft that had to be rebooted before a counter overflowed. When you fix a number's width, always ask whether the biggest possible answer still fits.

Happily, no. Computers almost never build separate subtracting circuitry. Instead they store negative numbers in a clever form called two's complement, which turns a - b into a + (-b) — an addition. So the same four rules and the same adder hardware you've just learned do subtraction too. That is why addition is the beating heart of the processor. Two's complement is a lesson of its own.