DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Binary Arithmetic by Hand: The Foundation Every Developer Should Understand

I had been writing code for three years before I actually understood what happens when you add two numbers in binary. I knew that computers used ones and zeros. I could vaguely wave my hands about bits and bytes. But if you had asked me to add 10110 and 01101 on a whiteboard, I would have stared at you blankly.

Then I started debugging a networking issue involving subnet masks and bitwise operations, and my lack of foundational knowledge caught up with me hard. I spent two days on a problem that would have taken twenty minutes if I had understood binary arithmetic.

Why Binary Exists

Computers use binary because transistors have two stable states: on and off, high voltage and low voltage, 1 and 0. Everything your computer does -- every calculation, every pixel, every network packet -- is ultimately a sequence of binary operations performed on binary numbers.

When you write let x = 42 in JavaScript, the computer stores the number 42 as 00101010 in memory. When you write x + 17, the CPU performs binary addition on 00101010 and 00010001. The result, 00111011, gets translated back to 59 for you.

Understanding this layer does not make you a better coder every day. But when it matters, it really matters.

Binary Addition Rules

Binary addition follows four rules. That is it. Four rules that govern every calculation your processor executes:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (0, carry 1)
Enter fullscreen mode Exit fullscreen mode

The last one is the important one. In decimal, 5 + 5 = 10 (we write 0 and carry 1). In binary, 1 + 1 = 10 (we write 0 and carry 1). Same concept, smaller base.

Let us work through an example. Adding 13 (1101) and 11 (1011):

    1 1 0 1   (13)
  + 1 0 1 1   (11)
  ---------
Carry: 1 1 1
Result: 1 1 0 0 0   (24)
Enter fullscreen mode Exit fullscreen mode

Step by step from right to left:

  • Column 1: 1 + 1 = 0, carry 1
  • Column 2: 0 + 1 + carry 1 = 0, carry 1
  • Column 3: 1 + 0 + carry 1 = 0, carry 1
  • Column 4: 1 + 1 + carry 1 = 1, carry 1
  • Column 5: carry 1 = 1

Result: 11000 = 24 in decimal. Correct.

Binary Subtraction and Two's Complement

Subtraction in binary is where things get interesting. Computers do not actually subtract. Instead, they add the negative of a number using a representation called two's complement.

To negate a binary number in two's complement:

  1. Flip all the bits (0 becomes 1, 1 becomes 0). This is the one's complement.
  2. Add 1.

For example, to represent -5 in an 8-bit system:

5 in binary:       00000101
Flip all bits:     11111010
Add 1:             11111011   (-5 in two's complement)
Enter fullscreen mode Exit fullscreen mode

Now subtraction becomes addition:

13 - 5  =  13 + (-5)
00001101 + 11111011 = 00001000 = 8
Enter fullscreen mode Exit fullscreen mode

The carry out of the most significant bit is discarded, and you get 8. This is elegant because it means the CPU only needs addition circuits. No separate subtraction hardware required.

Multiplication and Division

Binary multiplication works like long multiplication in decimal, but simpler because you are only ever multiplying by 0 or 1:

    1 1 0 1   (13)
  x   1 0 1   (5)
  ---------
    1 1 0 1   (13 x 1)
  0 0 0 0 0   (13 x 0, shifted left 1)
1 1 0 1 0 0   (13 x 1, shifted left 2)
-----------
1 0 0 0 0 0 1  (65)
Enter fullscreen mode Exit fullscreen mode

Binary division uses repeated subtraction and shifting, similar to long division. In practice, CPUs use optimized algorithms like Newton-Raphson for division because it is the slowest basic arithmetic operation.

Where This Shows Up in Real Code

Bitwise operations in programming are binary arithmetic:

// AND: both bits must be 1
13 & 11   // 1101 & 1011 = 1001 = 9

// OR: either bit must be 1
13 | 11   // 1101 | 1011 = 1111 = 15

// XOR: bits must differ
13 ^ 11   // 1101 ^ 1011 = 0110 = 6

// Left shift: multiply by 2
13 << 1   // 11010 = 26

// Right shift: divide by 2
13 >> 1   // 110 = 6
Enter fullscreen mode Exit fullscreen mode

These operations are constant-time and used extensively in graphics programming, cryptography, compression algorithms, network protocols, and systems programming.

Three Tips for Getting Comfortable

1. Practice converting small numbers. Memorize the powers of 2 up to 256 (1, 2, 4, 8, 16, 32, 64, 128, 256). That makes mental binary conversion almost instant for small numbers.

2. Use binary arithmetic to verify your bitwise operations. When a bitwise expression in your code is not producing the expected result, write out the binary by hand. The bug becomes obvious almost immediately.

3. Check your work with a tool. When I am working through binary calculations for subnet masks or bit manipulation problems, I use the binary calculator at zovo.one to verify my manual work. It handles addition, subtraction, multiplication, and division in binary, and it has saved me from plenty of carry-bit mistakes.

I am Michael Lip. I build free developer tools at zovo.one. 350+ tools, all private, all free.

Top comments (0)