codeworking.org
Search
Developer Skill / Gist

Deep Dive into Binary Arithmetic

How Computers Calculate: Every digital processor on Earth—from the simplest microcontroller to the most powerful AI supercomputer—performs complex mathematical calculations using simple electrical switches. By visualizing Binary digits as a row of lamps (where 💡 ON = 1 and ⚪ OFF = 0), we can demystify how silicon chips add, subtract, and handle negative numbers using pure addition and Two’s Complement logic.

(To master the basics of place values and positional math first, see our Deep Dive into Number Systems.)


1. The Physical Mental Model: 4 and 8 Lamps

Inside a CPU, memory registers are arrays of tiny electronic switches (transistors) holding electrical charge. We can visualize registers as a row of lamps:

+-----------------------------------------------------------------------------------+
|                           THE 4-LAMP REGISTER (4 BITS = 1 NIBBLE)                 |
+-----------------------------------------------------------------------------------+
  Lamp Index:         Lamp 3        Lamp 2        Lamp 1        Lamp 0 (LSB)
  Binary Weight:      2³ = 8        2² = 4        2¹ = 2        2⁰ = 1
  Physical State:     [ 💡 ON ]     [ ⚪ OFF ]    [ 💡 ON ]     [ 💡 ON ]
  Bit Values:            1             0             1             1
  Total Decimal:      (1 × 8)   +   (0 × 4)   +   (1 × 2)   +   (1 × 1)  =  11
+-----------------------------------------------------------------------------------+
+-----------------------------------------------------------------------------------+
|                           THE 8-LAMP REGISTER (8 BITS = 1 BYTE)                   |
+-----------------------------------------------------------------------------------+
  Weights:    128     64     32     16      8      4      2      1
  Lamps:     [ ⚪ ]  [ 💡 ] [ ⚪ ] [ 💡 ] [ ⚪ ] [ ⚪ ] [ 💡 ] [ 💡 ]
  Bits:        0      1      0      1      0      0      1      1
  Decimal:     0   + 64   +  0   + 16   +  0   +  0   +  2   +  1  =  83
+-----------------------------------------------------------------------------------+
  • 4 Lamps (Nibble): Can display numbers from 0 (0000) to 15 (1111).
  • 8 Lamps (Byte): Can display numbers from 0 (00000000) to 255 (11111111).

2. Binary Addition (+) with Lamps

Binary addition follows four simple physical rules based on lamp states:

+-----------------------------------------------------------------------------------+
|                           THE 4 RULES OF BINARY ADDITION                          |
+-----------------------------------------------------------------------------------+
  1. ⚪ OFF (0) + ⚪ OFF (0)  =  ⚪ OFF (0)
  2. ⚪ OFF (0) + 💡 ON  (1)  =  💡 ON  (1)
  3. 💡 ON  (1) + ⚪ OFF (0)  =  💡 ON  (1)
  4. 💡 ON  (1) + 💡 ON  (1)  =  ⚪ OFF (0), with a CARRY 💡 (1) to the left lamp!
  5. 💡 (1) + 💡 (1) + 💡 (1) =  💡 ON  (1), with a CARRY 💡 (1) to the left lamp!
+-----------------------------------------------------------------------------------+

💡 The Carry Rule Explained: Just as 9 + 1 = 10 in decimal (the current column resets to 0 and carries 1 to the tens column), in binary 1 + 1 = 10₂ (the current lamp resets to OFF 0, and carries a glowing pulse to the next lamp on the left!).


Step-by-Step 4-Lamp Addition Example: 5 + 3 = 8

Let us add Decimal 5 (0101) and Decimal 3 (0011):

       Carry Row:       ¹   ¹          (Carry pulses propagate left)
  Number A (5):     [0] [1] [0] [1]    (4 + 1)
  Number B (3):  +  [0] [0] [1] [1]    (2 + 1)
  ---------------------------------
  Result (8):       [1] [0] [0] [0]    (8 + 0 + 0 + 0 = 8!)

Detailed Column Breakdown:

  1. Lamp 0 (2⁰ = 1): 1 + 1 = 0, carry 1 to Lamp 1. (Lamp 0 turns OFF).
  2. Lamp 1 (2¹ = 2): 0 + 1 + carry 1 = 0, carry 1 to Lamp 2. (Lamp 1 turns OFF).
  3. Lamp 2 (2² = 4): 1 + 0 + carry 1 = 0, carry 1 to Lamp 3. (Lamp 2 turns OFF).
  4. Lamp 3 (2³ = 8): 0 + 0 + carry 1 = 1. (Lamp 3 turns ON).
  • Final Lamps: [💡] [⚪] [⚪] [⚪] = 8!

3. How Silicon Circuits Add: Half & Full Adders

In physical CPUs, binary addition is built using elementary logic gates:

+-----------------------------------------------------------------------------------+
|                               1-BIT FULL ADDER CIRCUIT                            |
+-----------------------------------------------------------------------------------+
  Input Bit A ───┬───────────────────┐
                 │                   v
                 │                [ XOR ] ───┬──────────────┐
  Input Bit B ───┼──────────┬────────┘       │              v
                 │          │                │           [ XOR ] ───► Sum Bit (S)
  Carry In (Cin)─┼──────────┼────────────────┼───────────┘
                 │          │                v
                 │          │             [ AND ] ───┐
                 │          │                        v
                 v          v                     [ OR ] ────► Carry Out (Cout)
              [     AND     ] ───────────────────┘
+-----------------------------------------------------------------------------------+
  1. Half Adder: Uses one XOR gate (to compute the sum lamp: A ^ B) and one AND gate (to compute the carry lamp: A & B).
  2. Full Adder: Chained together to handle an incoming carry from the previous lamp column. Chaining 8 full adders creates an 8-bit Ripple-Carry Adder.

4. How Computers Subtract (-) Using Two’s Complement

A monumental breakthrough in computer engineering was realizing that CPUs do not need separate subtraction hardware. Instead, subtraction A - B is mathematically computed as addition with a negative number:

A - B  <===>  A + (-B)

4.1 How to Represent Negative Numbers with Lamps (Two’s Complement)

In an 8-lamp signed register:

  • The leftmost lamp (Lamp 7) is the Sign Lamp:
    • ⚪ OFF (0) ➔ Positive Number
    • 💡 ON (1) ➔ Negative Number (with negative weight -128)
+-----------------------------------------------------------------------------------+
|                        THE TWO-STEP TWO'S COMPLEMENT ALGORITHM                    |
+-----------------------------------------------------------------------------------+
  To turn any positive number into its negative equivalent:
  
  Step 1: INVERT ALL LAMPS (Flip 💡 ON ──> ⚪ OFF, and ⚪ OFF ──> 💡 ON)
  Step 2: ADD 1 to the result (turn on the lowest lamp with carry propagation)
+-----------------------------------------------------------------------------------+

Example: How to Represent -3 with 4 Lamps

  1. Start with +3: [0] [0] [1] [1]
  2. Step 1 (Invert all lamps): [1] [1] [0] [0] (One’s Complement)
  3. Step 2 (Add 1): [1] [1] [0] [1]
  • Verification: (-8) + 4 + 0 + 1 = -3!

4.2 Subtraction in Action: 7 - 3 = 4

Instead of subtracting 3 from 7, the CPU computes: 7 + (-3):

       Carry Row:   ¹   ¹   ¹          (Carry pulses propagate left)
  Number A (+7):    [0] [1] [1] [1]
  Number B (-3): +  [1] [1] [0] [1]    (Two's complement of 3)
  ---------------------------------
  Raw Sum:       (1)[0] [1] [0] [0]

                  └──> Leftmost carry overflows beyond the 4 lamps and is dropped!
  
  Final 4 Lamps:    [0] [1] [0] [0]    (4 + 0 + 0 = 4!)

The result is exactly +4 (0100)! By simply inverting bits and adding, the standard addition circuit performed flawless subtraction.


4.3 Subtraction Resulting in a Negative Number: 3 - 7 = -4

Let us compute 3 + (-7) with 4 lamps:

  1. +7 = 0111 ➔ Invert 1000 ➔ Add 1 = 1001 (-7).
  2. Add +3 (0011) and -7 (1001):
  Number A (+3):    [0] [0] [1] [1]
  Number B (-7): +  [1] [0] [0] [1]
  ---------------------------------
  Result:           [1] [1] [0] [0]

Interpreting the Result 1100:

  • Leftmost sign lamp is ON (1) ➔ Negative number!
  • Value: (-8) + 4 + 0 + 0 = -4!

5. Register Overflow & The CPU Flags

What happens when two large numbers are added and the sum exceeds the available lamps?

  8-Lamp Register Max Value: 255 (Unsigned) or +127 (Signed)

  Adding: 200 (`11001000`) + 100 (`01100100`) = 300 (`100101100`)

Because there are only 8 physical lamps, the 9th bit spills into a special single-lamp indicator called the Carry Flag (CF) in the CPU’s Status Register:

  • Carry Flag (CF): Set to 1 when unsigned arithmetic overflows the register width.
  • Overflow Flag (OF): Set to 1 when signed arithmetic produces an incorrect sign (e.g. adding two positive numbers produces a negative result).

6. Summary & Arithmetic Cheat Sheet

+───────────────────────────────────────────────────────────────────────────────────+
|                           BINARY ARITHMETIC CHEAT SHEET                           |
+───────────────────────────────────────────────────────────────────────────────────+
  Addition:        0 + 0 = 0
                   0 + 1 = 1
                   1 + 1 = 0 (carry 1)
                   1 + 1 + 1 = 1 (carry 1)

  Subtraction:     A - B  ==  A + (NOT B + 1)

  Two's Comp:      1. Flip all bits (0 ──> 1, 1 ──> 0)
                   2. Add 1 to lowest bit
+───────────────────────────────────────────────────────────────────────────────────+
Operation Equation 4-Lamp Binary Decimal Result
Simple Addition 4 + 2 0100 + 0010 = 0110 6
Carry Propagation 5 + 3 0101 + 0011 = 1000 8
Positive Subtraction 7 - 3 0111 + 1101 = [1]0100 4
Negative Subtraction 3 - 5 0011 + 1011 = 1110 -2

By combining simple on/off voltage states with Boolean logic gates and Two’s Complement arithmetic, computing hardware executes billions of calculations per second with flawless mathematical precision.

S

Computer Science educator, Software Engineer, Cloud Computing & Cloud Native Architect, and AI/ML Engineer. Founder & Owner of unus.one, softwork.ing, and codeworking.org.

Comments & Discussion