Binary Arithmetic

The Complete Guide to Mathematical Operations in Base-2

Introduction: Why Binary Math Matters

Every calculation your computer performs—from rendering video games to training AI models—ultimately reduces to binary arithmetic. Understanding how computers add, subtract, multiply, and divide in base-2 isn't just academic; it's fundamental to understanding how digital technology actually works.

When you press keys on a calculator, when a game engine computes physics, when your phone encrypts a message, when a spacecraft navigates to Mars—all of these operations are performed through the manipulation of binary numbers using the techniques you'll master in this guide.

What Makes Binary Arithmetic Special

Binary arithmetic follows the same principles as decimal arithmetic that you learned in school, but with only two digits (0 and 1) instead of ten. This simplicity makes it easier to learn than decimal math—there are fewer rules to memorize. Once you internalize these rules, you'll see why computers can perform billions of calculations per second.

This guide will take you from absolute beginner to complete mastery. We'll explore every operation in depth, work through dozens of examples, examine how hardware actually implements these operations, and provide practice problems with detailed solutions. By the end, you'll have a deeper understanding of binary arithmetic than most computer science graduates.

2 Possible digits (0 and 1)
4 Basic operations
8 Rules total (vs 200 in decimal)
Calculations you can perform

Fundamentals: The Four Rules of Binary

Before diving into complex operations, let's establish the four fundamental rules that govern all binary arithmetic. In decimal, you had to memorize 100 addition facts (0+0 through 9+9) and 100 multiplication facts. In binary, you only need four rules for each operation.

The Complete Addition Rules

0 + 0 = 0
Zero plus zero equals zero
0 + 1 = 1
Zero plus one equals one
1 + 0 = 1
One plus zero equals one
1 + 1 = 10
One plus one equals two (written as "10" in binary: 0 with carry 1)

The fourth rule is the key to understanding binary arithmetic. When we add 1 + 1, we get 2. But in binary, we can't write "2"—we only have 0 and 1. So we write 2 as "10" in binary (just like we write "10" in decimal when we run out of single digits after 9).

The Complete Multiplication Rules

0 × 0 = 0
Zero times zero equals zero
0 × 1 = 0
Zero times one equals zero
1 × 0 = 0
One times zero equals zero
1 × 1 = 1
One times one equals one

Binary multiplication is even simpler than addition! Multiplying by 0 always gives 0, and multiplying by 1 always gives the original number. This simplicity is why computer multiplication can be implemented with simple logic gates.

The Decimal Parallel

Everything in binary arithmetic parallels decimal arithmetic. When you add 7 + 5 in decimal, you get 12—you write down 2 and carry the 1. In binary, when you add 1 + 1, you get 10—you write down 0 and carry the 1. The process is identical; only the base changes.

Binary Addition: The Foundation

Binary addition is the cornerstone of all computer arithmetic. Every other operation—subtraction, multiplication, even division—can be implemented using addition as a building block. Master this, and you've mastered the heart of computational mathematics.

Single-Bit Addition

Let's start with the simplest case: adding two single-bit numbers. This is called a half adder in digital logic because it handles two inputs but doesn't account for an incoming carry.

A B Sum Carry Decimal Equivalent
0 0 0 0 0 + 0 = 0
0 1 1 0 0 + 1 = 1
1 0 1 0 1 + 0 = 1
1 1 0 1 1 + 1 = 2 (binary: 10)

The carry bit is crucial. When two 1s are added, the sum column shows 0, but the carry column shows 1. Together, these form "10" in binary (decimal 2).

The Full Adder: Including the Carry Input

Real addition involves more than two bits at a time. We need to account for carries from previous positions. A full adder takes three inputs: two bits to add plus a carry-in from the previous position.

A B Carry In Sum Carry Out Total Value
0 0 0 0 0 0
0 0 1 1 0 1
0 1 0 1 0 1
0 1 1 0 1 2
1 0 0 1 0 1
1 0 1 0 1 2
1 1 0 0 1 2
1 1 1 1 1 3 (binary: 11)

Understanding the Full Adder

When adding 1 + 1 + 1 (the last row), we get 3 in decimal. In binary, 3 is "11"—so the sum is 1 and the carry-out is 1. This single full adder circuit, replicated for each bit position, allows computers to add numbers of any size.

Multi-Bit Addition with Carries

Now let's add multi-bit numbers. The process is identical to decimal addition: start from the rightmost bit, add each column, and carry when necessary.

Example 1: Adding 0101 + 0011 (5 + 3 = 8)

    Carry:    0 1 1 0
              -------
              0 1 0 1    (5 in decimal)
            + 0 0 1 1    (3 in decimal)
              -------
              1 0 0 0    (8 in decimal)
                        

Step-by-step:

  1. Position 0 (rightmost): 1 + 1 = 10. Write 0, carry 1.
  2. Position 1: 0 + 1 + carry(1) = 10. Write 0, carry 1.
  3. Position 2: 1 + 0 + carry(1) = 10. Write 0, carry 1.
  4. Position 3: 0 + 0 + carry(1) = 1. Write 1, no carry.

Result: 1000₂ = 8₁₀ ✓

Extensive Worked Examples

Let's work through progressively more complex examples to solidify your understanding.

Example 2: Adding 1011 + 1101 (11 + 13 = 24)

    Carry:  1 1 1 1 0
            ---------
              1 0 1 1    (11 in decimal)
            + 1 1 0 1    (13 in decimal)
            ---------
            1 1 0 0 0    (24 in decimal)
                        

Step-by-step:

  1. Position 0: 1 + 1 = 10. Write 0, carry 1.
  2. Position 1: 1 + 0 + carry(1) = 10. Write 0, carry 1.
  3. Position 2: 0 + 1 + carry(1) = 10. Write 0, carry 1.
  4. Position 3: 1 + 1 + carry(1) = 11. Write 1, carry 1.
  5. Position 4: 0 + 0 + carry(1) = 1. Write 1.

Result: 11000₂ = 24₁₀ ✓

Example 3: Adding 11111111 + 00000001 (255 + 1 = 256)

    Carry:  1 1 1 1 1 1 1 1 0
            -----------------
              1 1 1 1 1 1 1 1    (255 in decimal)
            + 0 0 0 0 0 0 0 1    (1 in decimal)
            -----------------
            1 0 0 0 0 0 0 0 0    (256 in decimal)
                        

Analysis: This example shows the "cascade carry" phenomenon. Adding 1 to 11111111 causes a carry that ripples through all 8 bit positions. Every position produces a 0 with a carry until we reach the 9th position.

Result: 100000000₂ = 256₁₀ ✓

Important: This demonstrates why 8 bits can only represent values 0-255. Adding 1 to 255 requires a 9th bit!

Example 4: Adding 10110110 + 01011011 (182 + 91 = 273)

    Carry:  1 0 1 1 1 0 1 0 0
            -----------------
              1 0 1 1 0 1 1 0    (182 in decimal)
            + 0 1 0 1 1 0 1 1    (91 in decimal)
            -----------------
            1 0 0 0 1 0 0 0 1    (273 in decimal)
                        

Verification:

  • 182 = 128 + 32 + 16 + 4 + 2 = 10110110₂ ✓
  • 91 = 64 + 16 + 8 + 2 + 1 = 01011011₂ ✓
  • 273 = 256 + 16 + 1 = 100010001₂ ✓

Detecting Overflow in Addition

Overflow occurs when the result of an addition is too large to fit in the available bits. This is a critical concept in computer systems—failing to handle overflow causes bugs, security vulnerabilities, and system crashes.

Unsigned Overflow

For unsigned numbers, overflow occurs when there's a carry out of the most significant bit. If you're using 8 bits (max value 255) and add 200 + 100 = 300, you can't represent 300 in 8 bits. The result "wraps around" to 300 - 256 = 44.

Signed Overflow

For signed numbers (covered in our Signed Numbers guide), overflow is more complex. It occurs when adding two positive numbers gives a negative result, or adding two negative numbers gives a positive result.

Example: 8-bit Unsigned Overflow

              1 1 0 0 1 0 0 0    (200 in decimal)
            + 0 1 1 0 0 1 0 0    (100 in decimal)
            -----------------
          [1] 0 0 1 0 1 1 0 0
              ↑
           Overflow carry (lost in 8-bit system)
                        

The true result is 300 (100101100₂), but in an 8-bit system, the leading 1 is lost, leaving 00101100₂ = 44. This is called modular arithmetic or "wrap-around."

Overflow in Real Systems

The infamous Y2K bug was an overflow problem—years stored as 2-digit numbers would overflow from 99 to 00. Similarly, the "Year 2038 problem" affects systems that store time as a 32-bit signed integer: on January 19, 2038, at 03:14:07 UTC, this counter will overflow and wrap to a negative number (appearing as 1901).

Binary Subtraction: Borrowing in Base-2

Binary subtraction follows the same borrowing principle as decimal subtraction, but with only two digits to work with. While this might seem more restrictive, it actually makes the rules simpler and more systematic.

Basic Subtraction Rules

0 - 0 = 0
Zero minus zero equals zero
1 - 0 = 1
One minus zero equals one
1 - 1 = 0
One minus one equals zero
0 - 1 = 1 (borrow 1)
Zero minus one requires borrowing from the next position

The fourth rule is where subtraction gets interesting. When we need to subtract 1 from 0, we must borrow from the next higher position—just like in decimal when you subtract 7 from 3 in 43 - 17.

The Borrowing Process

In decimal, when you borrow, you take 10 from the next position. In binary, when you borrow, you take 2 (which is "10" in binary) from the next position. This makes the current position worth 2, so 0 becomes 2, and 2 - 1 = 1.

Understanding Binary Borrowing

    Simple case: 10 - 01 (2 - 1 = 1)

              1 0    (2 in decimal)
            - 0 1    (1 in decimal)
              ---

    Position 0: Need 0 - 1, can't do it!
    Borrow 1 from position 1, making position 0 worth 2:

              0 2    (after borrowing)
            - 0 1
              ---
              0 1    (1 in decimal) ✓
                        

Worked Examples

Example 1: 1010 - 0011 (10 - 3 = 7)

              1 0 1 0    (10 in decimal)
            - 0 0 1 1    (3 in decimal)
              -------

    Step by step:
    Position 0: 0 - 1 → Borrow from position 1
                Position 1 was 1, becomes 0
                Position 0 becomes 2: 2 - 1 = 1

    Position 1: 0 - 1 → Borrow from position 2
                Position 2 was 0, must borrow from position 3
                Position 3 was 1, becomes 0
                Position 2 becomes 2-1=1 (gave one to position 1)
                Position 1 becomes 2: 2 - 1 = 1

    Position 2: 1 - 0 = 1
    Position 3: 0 - 0 = 0

    Result:
              1 0 1 0
            - 0 0 1 1
              -------
              0 1 1 1    (7 in decimal) ✓
                        

Example 2: 11010 - 01011 (26 - 11 = 15)

    Borrow:     0 0 1 1 0
              -----------
                1 1 0 1 0    (26 in decimal)
              - 0 1 0 1 1    (11 in decimal)
              -----------

    Position 0: 0 - 1, borrow from 1. Pos 1: 1→0. Pos 0: 2-1=1
    Position 1: 0 - 1, borrow from 2. Pos 2: 0, borrow from 3.
                Pos 3: 1→0. Pos 2: 2-1=1. Pos 1: 2-1=1
    Position 2: 1 - 0 = 1
    Position 3: 0 - 1, borrow from 4. Pos 4: 1→0. Pos 3: 2-1=1
    Position 4: 0 - 0 = 0

    Result:
                1 1 0 1 0
              - 0 1 0 1 1
              -----------
                0 1 1 1 1    (15 in decimal) ✓
                        

Example 3: 10000000 - 00000001 (128 - 1 = 127)

              1 0 0 0 0 0 0 0    (128 in decimal)
            - 0 0 0 0 0 0 0 1    (1 in decimal)
              ---------------

    This requires a "cascade borrow" through all positions:
    Position 0: 0-1, borrow from 1
    Position 1: 0, borrow from 2 → becomes 1 after giving to 0
    Position 2: 0, borrow from 3 → becomes 1 after giving to 1
    ...continuing through all positions...
    Position 7: 1, gives to 6 → becomes 0

    Result:
              1 0 0 0 0 0 0 0
            - 0 0 0 0 0 0 0 1
              ---------------
              0 1 1 1 1 1 1 1    (127 in decimal) ✓
                        

Pattern Recognition: Notice that 2ⁿ - 1 always produces all 1s in binary. 128 - 1 = 127 = 01111111. This pattern (2ⁿ - 1 = 111...1) is fundamental in computing, representing the maximum value for n bits.

Subtraction Using Complements

Here's a powerful secret: computers rarely perform subtraction using borrowing! Instead, they convert subtraction to addition using two's complement. This allows the same circuitry to handle both addition and subtraction.

Two's Complement Subtraction

To subtract B from A: (1) Flip all bits of B (one's complement), (2) Add 1 to get two's complement, (3) Add the result to A. This method is covered in depth in our Signed Numbers guide.

Example: 1010 - 0011 using Two's Complement

    A = 1010 (10)
    B = 0011 (3)

    Step 1: One's complement of B (flip all bits)
            0011 → 1100

    Step 2: Add 1 to get two's complement
            1100 + 1 = 1101

    Step 3: Add to A
              1 0 1 0
            + 1 1 0 1
            --------
          [1] 0 1 1 1

    Ignore the overflow carry (in 4-bit arithmetic)
    Result: 0111 = 7 ✓
                        

The result matches our earlier calculation! Two's complement allows subtraction using only addition circuits.

Binary Multiplication: Multiple Methods

Binary multiplication is remarkably elegant. Since we're only multiplying by 0 or 1, each partial product is either all zeros (×0) or the original number (×1). This simplicity enables efficient hardware implementation.

Multiplication Fundamentals

Recall the multiplication rules:

  • 0 × 0 = 0
  • 0 × 1 = 0
  • 1 × 0 = 0
  • 1 × 1 = 1

This means each partial product in long multiplication is either zero or a shifted copy of the multiplicand!

Shift and Add Method

Binary multiplication using the traditional long multiplication approach becomes a simple "shift and add" operation:

Example 1: 101 × 11 (5 × 3 = 15)

            1 0 1        (5 in decimal)
          ×   1 1        (3 in decimal)
          -------
            1 0 1        (101 × 1, position 0)
          1 0 1          (101 × 1, position 1, shifted left)
          -------
          1 1 1 1        (15 in decimal)
                        

Process:

  1. Multiply 101 by the rightmost bit (1): Result is 101
  2. Multiply 101 by the next bit (1): Result is 101, shifted left one position (1010)
  3. Add the partial products: 0101 + 1010 = 1111

Result: 1111₂ = 15₁₀ ✓

Example 2: 1101 × 1011 (13 × 11 = 143)

              1 1 0 1            (13 in decimal)
            ×   1 0 1 1          (11 in decimal)
            -----------
              1 1 0 1            (1101 × 1, position 0)
            1 1 0 1              (1101 × 1, position 1)
          0 0 0 0                (1101 × 0, position 2)
        1 1 0 1                  (1101 × 1, position 3)
            -----------
        1 0 0 0 1 1 1 1          (143 in decimal)
                        

Adding partial products:

              0 0 0 0 1 1 0 1    (partial product 0)
            + 0 0 0 1 1 0 1 0    (partial product 1)
            + 0 0 0 0 0 0 0 0    (partial product 2)
            + 1 1 0 1 0 0 0 0    (partial product 3)
            -----------------
              1 0 0 0 1 1 1 1    (143 in decimal) ✓
                            

Comprehensive Examples

Example 3: 10110 × 10011 (22 × 19 = 418)

                1 0 1 1 0        (22 in decimal)
              ×   1 0 0 1 1      (19 in decimal)
              -------------
                1 0 1 1 0        (×1, position 0)
              1 0 1 1 0          (×1, position 1)
            0 0 0 0 0            (×0, position 2)
          0 0 0 0 0              (×0, position 3)
        1 0 1 1 0                (×1, position 4)
              -------------
                        

Full addition of partial products:

          0 0 0 0 0 1 0 1 1 0    Position 0
        + 0 0 0 0 1 0 1 1 0 0    Position 1
        + 0 0 0 0 0 0 0 0 0 0    Position 2
        + 0 0 0 0 0 0 0 0 0 0    Position 3
        + 1 0 1 1 0 0 0 0 0 0    Position 4
        ---------------------
          1 1 0 1 0 0 0 1 0      (418 in decimal) ✓
                            

Verification: 418 = 256 + 128 + 32 + 2 = 110100010₂ ✓

Multiplication by Powers of 2

Multiplying by powers of 2 is trivially simple in binary: just shift left! Each left shift multiplies by 2.

Example: Multiplying by 2, 4, and 8

    Original:     0 0 1 0 1    =  5
    × 2 (shift 1): 0 1 0 1 0    = 10
    × 4 (shift 2): 1 0 1 0 0    = 20
    × 8 (shift 3): 1 0 1 0 0 0  = 40
                        

This is why programmers often use bit shifts for fast multiplication. x << 3 is much faster than x * 8 in most situations, though modern compilers often make this optimization automatically.

Booth's Algorithm (Advanced)

For signed binary multiplication and more efficient handling of consecutive 1s, computers often use Booth's Algorithm, developed by Andrew Booth in 1950. This algorithm reduces the number of additions by treating consecutive 1s as a subtraction and addition pair.

Booth's Insight

Consider multiplying by 01111110 (126). Standard multiplication requires 6 additions (one for each 1). Booth recognized that 01111110 = 10000000 - 00000010 (128 - 2). So instead of 6 additions, we can do one addition and one subtraction (which is still an addition with negation).

The algorithm examines pairs of adjacent bits and performs:

  • 01: Add the multiplicand (start of a string of 1s)
  • 10: Subtract the multiplicand (end of a string of 1s)
  • 00 or 11: Do nothing (continue a string)

Booth's Algorithm Example: 0110 × 0011 (6 × 3 = 18)

    Multiplier: 0011, append 0 → 00110
    Examine pairs from right to left:

    Position 0: 10 → Subtract (start of 1s)
    Position 1: 11 → Nothing (continuing 1s)
    Position 2: 01 → Add (end of 1s)
    Position 3: 00 → Nothing

    -0110 at position 0: -6
    +0110 at position 2: +24 (shifted left 2)

    Result: -6 + 24 = 18 ✓
                        

Binary Division: Long Division Mastery

Binary division uses the same long division algorithm you learned for decimal numbers, but again, it's simpler! Each step only asks: "Does the divisor fit?" The answer is always just 1 (yes) or 0 (no).

Division Fundamentals

In any division A ÷ B = Q remainder R:

  • A = Dividend (number being divided)
  • B = Divisor (number dividing by)
  • Q = Quotient (result)
  • R = Remainder (what's left over)

Binary Long Division

The algorithm is straightforward:

  1. Bring down bits from the dividend one at a time
  2. At each step, ask: "Is the divisor ≤ current partial dividend?"
  3. If yes: write 1 in quotient, subtract divisor from partial dividend
  4. If no: write 0 in quotient
  5. Repeat until all bits are processed

Step-by-Step Examples

Example 1: 1010 ÷ 10 (10 ÷ 2 = 5)

            0 1 0 1     ← Quotient
          +-------
    10   | 1 0 1 0     ← Dividend

    Step 1: Bring down 1
            Is 10 ≤ 1? No → Write 0

    Step 2: Bring down 0, partial dividend is 10
            Is 10 ≤ 10? Yes → Write 1, subtract
            10 - 10 = 0

    Step 3: Bring down 1, partial dividend is 01
            Is 10 ≤ 01? No → Write 0

    Step 4: Bring down 0, partial dividend is 10
            Is 10 ≤ 10? Yes → Write 1, subtract
            10 - 10 = 0

    Quotient: 0101 = 5
    Remainder: 0 ✓
                        

Example 2: 11011 ÷ 11 (27 ÷ 3 = 9)

              0 1 0 0 1     ← Quotient
            +----------
      11   | 1 1 0 1 1     ← Dividend

    Step 1: Bring down 1
            Is 11 ≤ 1? No → Write 0
            Partial: 1

    Step 2: Bring down 1, partial = 11
            Is 11 ≤ 11? Yes → Write 1, subtract
            11 - 11 = 00

    Step 3: Bring down 0, partial = 00
            Is 11 ≤ 00? No → Write 0
            Partial: 00

    Step 4: Bring down 1, partial = 01
            Is 11 ≤ 01? No → Write 0
            Partial: 01

    Step 5: Bring down 1, partial = 11
            Is 11 ≤ 11? Yes → Write 1, subtract
            11 - 11 = 0

    Quotient: 01001 = 9
    Remainder: 0 ✓
                        

Example 3: 10101 ÷ 11 (21 ÷ 3 = 7)

              0 0 1 1 1     ← Quotient
            +----------
      11   | 1 0 1 0 1     ← Dividend (21)

    Step 1: Bring down 1
            Is 11 ≤ 01? No → Write 0

    Step 2: Bring down 0, partial = 10
            Is 11 ≤ 10? No → Write 0

    Step 3: Bring down 1, partial = 101
            Is 11 ≤ 101? Yes → Write 1
            101 - 11 = 10

    Step 4: Bring down 0, partial = 100
            Is 11 ≤ 100? Yes → Write 1
            100 - 11 = 1

    Step 5: Bring down 1, partial = 11
            Is 11 ≤ 11? Yes → Write 1
            11 - 11 = 0

    Quotient: 00111 = 7
    Remainder: 0 ✓

    Verification: 7 × 3 = 21 ✓
                        

Handling Remainders

Example 4: 10111 ÷ 100 (23 ÷ 4 = 5 remainder 3)

              0 0 1 0 1     ← Quotient
            +----------
     100   | 1 0 1 1 1     ← Dividend (23)

    Step 1: Bring down 1
            Is 100 ≤ 001? No → Write 0

    Step 2: Bring down 0, partial = 10
            Is 100 ≤ 010? No → Write 0

    Step 3: Bring down 1, partial = 101
            Is 100 ≤ 101? Yes → Write 1
            101 - 100 = 1

    Step 4: Bring down 1, partial = 11
            Is 100 ≤ 011? No → Write 0

    Step 5: Bring down 1, partial = 111
            Is 100 ≤ 111? Yes → Write 1
            111 - 100 = 11

    Quotient: 00101 = 5
    Remainder: 11 = 3 ✓

    Verification: 5 × 4 + 3 = 20 + 3 = 23 ✓
                        

Division by Powers of 2

Just as multiplication by powers of 2 is a left shift, division by powers of 2 is a right shift. Each right shift divides by 2 (discarding the remainder).

Example: Dividing by 2, 4, and 8

    Original:     1 0 1 1 0 0    = 44
    ÷ 2 (shift 1): 0 1 0 1 1 0   = 22
    ÷ 4 (shift 2): 0 0 1 0 1 1   = 11
    ÷ 8 (shift 3): 0 0 0 1 0 1   = 5 (remainder lost)
                        

The bit that "falls off" the right is the remainder. For 44 ÷ 8: quotient is 5, and the lost bits (100) represent remainder 4. Indeed, 44 = 5 × 8 + 4.

How Computers Actually Calculate

Understanding the hardware implementation of binary arithmetic reveals why certain operations are fast and others are slow, and why binary was chosen over other number systems.

The Arithmetic Logic Unit (ALU)

The ALU is the calculator inside your CPU. It performs all arithmetic and logical operations. A simple ALU might support:

  • ADD: Binary addition (foundation of most operations)
  • SUB: Implemented as ADD with two's complement
  • AND, OR, XOR, NOT: Bitwise logical operations
  • SHL, SHR: Shift left/right (multiply/divide by 2)

Ripple Carry vs. Carry Lookahead

A simple "ripple carry adder" chains full adders together, but each bit must wait for the carry from the previous bit. For 64 bits, this creates significant delay. Modern CPUs use "carry lookahead adders" that calculate all carries in parallel, dramatically increasing speed at the cost of more complex circuitry.

Multiplication in Hardware

Hardware multiplication typically uses one of these approaches:

Method Speed Complexity Usage
Shift-and-Add Slow (N cycles) Simple Microcontrollers
Booth's Algorithm Medium Moderate General purpose
Wallace Tree Fast (1-3 cycles) High Desktop/Server CPUs
Dadda Multiplier Very Fast Very High High-performance CPUs

Division in Hardware

Division is inherently more complex than multiplication. Most processors use iterative algorithms that take many clock cycles:

  • Restoring Division: Basic long division, tries subtraction and restores if negative
  • Non-Restoring Division: Avoids the restore step, faster
  • SRT Division: Uses lookup tables for faster quotient digit selection

Why Division is Slow

On most CPUs, division takes 20-100+ clock cycles, while multiplication takes 3-10 cycles and addition takes 1 cycle. This is why compilers optimize divisions by constants into multiplications and shifts when possible. For example, x / 3 might become (x * 2863311531) >> 33 which is faster!

Floating Point Arithmetic

For non-integer numbers, computers use floating-point representation (covered in our Data Representation guide). Floating-point arithmetic is more complex:

  1. Align the exponents of both numbers
  2. Perform the operation on the mantissas
  3. Normalize the result
  4. Round to fit the available precision
  5. Handle special cases (overflow, underflow, NaN)

This is why modern CPUs have dedicated Floating Point Units (FPUs) with their own specialized circuitry.

Understanding Overflow and Underflow

Overflow and underflow are critical concepts that every programmer must understand. They've caused rocket explosions (Ariane 5), game-breaking bugs, and countless security vulnerabilities.

Types of Overflow

Unsigned Overflow

Occurs when the result exceeds the maximum representable value. For 8 bits: 255 + 1 = 0 (wraps around). For 16 bits: 65535 + 1 = 0.

Signed Overflow

Occurs when the result exceeds the representable range. For 8-bit signed: 127 + 1 = -128. Adding two positives gives negative, or adding two negatives gives positive.

Multiplication Overflow

The product of two n-bit numbers can require 2n bits. Multiplying two 32-bit numbers can produce a 64-bit result that doesn't fit in 32 bits.

Real-World Overflow Disasters

Ariane 5 Explosion (1996)

A 64-bit floating-point number was converted to a 16-bit signed integer. The value was larger than 32,767 (max for 16-bit signed), causing overflow. The resulting bad data caused the rocket's computers to issue maximum thrust corrections, tearing the rocket apart. Cost: $370 million.

Pac-Man Kill Screen (1980)

The level counter is stored as an 8-bit unsigned integer. At level 256, it overflows to 0, causing the game to try to draw 256 fruits, corrupting half the screen with random tiles. This is the famous "kill screen."

Civilization Gandhi Bug (1991)

Gandhi's aggression was set to 1 (very peaceful). When democracy was adopted, aggression dropped by 2. The unsigned 8-bit value underflowed: 1 - 2 = 255 (maximum aggression). Result: Nuclear Gandhi.

Detecting and Preventing Overflow

Method Approach Tradeoff
Check Before Verify operands won't overflow before operating Adds overhead, complex logic
Check After Use CPU flags or compare result to operands Overflow already occurred
Wider Types Use 64-bit for 32-bit operations Memory/performance cost
Saturating Arithmetic Clamp to max/min instead of wrapping Silent incorrect results
Arbitrary Precision Use unlimited-size integers (BigInt) Significant performance cost

Practice Problems with Solutions

Test your understanding with these practice problems. Try to solve each one before revealing the solution.

Addition Problems

Problem 1: Add 11011 + 10101

Show Solution
    Carry: 1 1 0 1 0 0
           -----------
             1 1 0 1 1    (27)
           + 1 0 1 0 1    (21)
           -----------
           1 1 0 0 0 0    (48) ✓
                            

27 + 21 = 48

Problem 2: Add 10011011 + 01110011

Show Solution
    Carry: 1 0 1 1 0 1 1 0
           ---------------
             1 0 0 1 1 0 1 1    (155)
           + 0 1 1 1 0 0 1 1    (115)
           ---------------
           1 0 0 0 0 1 1 1 0    (270) ✓
                            

155 + 115 = 270 (requires 9 bits)

Subtraction Problems

Problem 3: Subtract 10101 - 01110

Show Solution
             1 0 1 0 1    (21)
           - 0 1 1 1 0    (14)
           -----------
             0 0 1 1 1    (7) ✓
                            

21 - 14 = 7

Problem 4: Subtract 11000000 - 00101010

Show Solution
             1 1 0 0 0 0 0 0    (192)
           - 0 0 1 0 1 0 1 0    (42)
           -----------------
             1 0 0 1 0 1 1 0    (150) ✓
                            

192 - 42 = 150

Multiplication Problems

Problem 5: Multiply 1101 × 101

Show Solution
              1 1 0 1        (13)
            ×   1 0 1        (5)
            ---------
              1 1 0 1        (×1)
            0 0 0 0          (×0)
          1 1 0 1            (×1)
            ---------
          1 0 0 0 0 0 1      (65) ✓
                            

13 × 5 = 65

Problem 6: Multiply 10110 × 1011

Show Solution
              1 0 1 1 0        (22)
            ×   1 0 1 1        (11)
            -----------
              1 0 1 1 0        (×1)
            1 0 1 1 0          (×1)
          0 0 0 0 0            (×0)
        1 0 1 1 0              (×1)
            -----------
        1 1 1 1 0 0 1 0        (242) ✓
                            

22 × 11 = 242

Division Problems

Problem 7: Divide 11110 ÷ 101

Show Solution
                0 0 1 1 0     ← Quotient (6)
              +---------
       101   | 1 1 1 1 0     ← Dividend (30)

       Step-by-step division...

       Quotient: 110 = 6
       Remainder: 0
                            

30 ÷ 5 = 6 remainder 0 ✓

Problem 8: Divide 101101 ÷ 111

Show Solution
                0 0 0 1 1 0   ← Quotient (6)
              +-----------
       111   | 1 0 1 1 0 1   ← Dividend (45)

       Quotient: 110 = 6
       Remainder: 011 = 3
                            

45 ÷ 7 = 6 remainder 3 ✓

Verification: 6 × 7 + 3 = 42 + 3 = 45 ✓

Real-World Applications

Binary arithmetic isn't just academic—it's used constantly in practical computing. Here are some common applications:

Graphics and Games

  • Color Manipulation: Each color channel (R, G, B) is 8 bits (0-255). Adding colors, blending, and adjusting brightness all use binary arithmetic.
  • Texture Coordinates: UV coordinates often use fixed-point binary arithmetic for precision and speed.
  • Physics Engines: Collision detection and physics calculations rely heavily on fast binary operations.

Cryptography

  • RSA Encryption: Uses modular exponentiation of very large binary numbers (2048+ bits).
  • Hash Functions: SHA-256 performs thousands of binary additions, rotations, and XOR operations.
  • Random Number Generation: Many PRNGs use binary multiplication and addition with carefully chosen constants.

Networking

  • IP Address Math: Subnet calculations use binary AND operations with subnet masks.
  • Checksums: Error detection codes use binary addition with one's complement.
  • Protocol Headers: Packing multiple values into bit fields requires binary arithmetic.

Embedded Systems

  • Sensor Data: ADC (Analog-to-Digital Converter) output is binary and often needs scaling.
  • PWM Control: Duty cycle calculations for motors and LEDs use binary division.
  • Fixed-Point Math: Microcontrollers without FPUs use binary arithmetic for "decimal" calculations.

The Binary Foundation

Every complex calculation—from training neural networks to rendering 3D graphics—ultimately reduces to the simple binary operations covered in this guide. The elegance of binary arithmetic is that these few simple rules, applied billions of times per second, can produce infinite complexity.

Summary

You've now mastered binary arithmetic—the mathematical foundation of all digital computing. From single-bit addition to complex division, from hardware implementation to overflow handling, you understand how computers actually calculate.

Key takeaways:

  • Binary arithmetic uses only 4 rules per operation (vs. 100 in decimal)
  • Carries and borrows work identically to decimal, just with 2 as the base
  • Subtraction can be implemented as addition using two's complement
  • Multiplication is shift-and-add; division is compare-and-subtract
  • Powers of 2 multiply/divide are simple bit shifts
  • Overflow is a critical concern that has caused real-world disasters

With this knowledge, you're prepared to understand how any calculation your computer performs actually works at the lowest level. Continue your journey with signed number representation, hexadecimal notation, and data representation.