Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

From Logic Gates to Adders

From Logic Gates to Adders

Computers don't "do arithmetic" — they just stack logic gates like building blocks. In this lesson we build, by hand, a circuit that can add using XOR, AND, and OR gates, going from half adders to full adders to a 4-bit adder, level by level.


Addition in daily life — the lesson of column addition

Recall how you add on paper. Compute 37 + 58:

First the ones place: 7 + 8 = 15, write 5, carry 1.

Then the tens place: 3 + 5 + carry 1 = 9, write 9, the result is 95.

This process reveals three key points:

  1. Digit-by-digit computation: each digit only cares about its own two addends and the carry from the lower digit.

  2. Carry propagation: a lower digit may produce a carry that must be passed to the higher digit.

  3. The lowest digit is special: it has no carry from a lower digit, so it only handles the two addends.

Computers add the same way: split a big problem (multi-digit addition) into many small ones (1-bit addition) and solve them one by one.

Half adder — 1-bit addition for the lowest digit

The lowest digit has no carry input and only needs to add two 1-bit binary numbers. A circuit that handles this is called a half adder (Half Adder).

A half adder has two inputs A and B, and two outputs: the sum bit Sum and the carry Carry.

Half adder truth table:

A B Sum Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

Looking at the truth table, an amazing coincidence appears:

Sum = A XOR B. When A and B differ, Sum=1; when they are the same, Sum=0 — exactly the truth table of an XOR gate.

Carry = A AND B. A carry is produced only when both inputs are 1 — exactly the truth table of an AND gate.

A half adder needs just 1 XOR gate + 1 AND gate. George Boole probably never imagined that the algebra system he invented would one day become the mathematical foundation of adders.

Full adder — complete addition that handles carries

The half adder has a fatal flaw: it cannot accept a carry from a lower digit. Except for the lowest digit, every digit must handle three numbers: A, B, and the carry-in Cin from the lower digit.

A circuit that handles two addends plus a carry input is called a full adder (Full Adder).

Full adder truth table:

A B Cin Sum Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1

A clever design: two half adders can build a full adder.

Step 1: half adder 1 processes A and B, producing intermediate sum S1 and carry C1.

Step 2: half adder 2 processes S1 and Cin, producing the final Sum and carry C2.

Step 3: final carry Cout = C1 OR C2 (if either half adder produces a carry, the final carry is output).

Full adder structure: 2 half adders + 1 OR gate. Layer upon layer, each half adder is itself XOR + AND. This hierarchical idea runs through all of computer architecture.

Cascading full adders — a 4-bit ripple-carry adder

With full adders, multi-bit addition is simple: chain several full adders, connecting the lower digit's carry-out to the higher digit's carry-in.

The cascade structure of a 4-bit adder:

Bit 0 (lowest): full adder handles A0, B0, carry-in = 0, producing sum S0 and carry C0.

Bit 1: full adder handles A1, B1, carry-in = C0, producing sum S1 and carry C1.

Bit 2: full adder handles A2, B2, carry-in = C1, producing sum S2 and carry C2.

Bit 3 (highest): full adder handles A3, B3, carry-in = C2, producing sum S3 and carry C3 (the final carry).

The carry ripples from lower to higher bits like a wave, so this structure is called a ripple carry adder (Ripple Carry Adder).

The ripple-carry adder is simple and intuitive but has a performance bottleneck: in the worst case the carry must propagate from bit 0 all the way to the highest bit, through N stages of full-adder delay. Modern CPUs optimize with techniques like carry lookahead (Carry Lookahead), but understanding ripple carry is the prerequisite for understanding all adder optimizations.

Python code demo

Example

============================================

Lesson 8: From Logic Gates to Adders - Python demo

runoob tutorial series

============================================

def

half_adder

(

a

,

b

)

:

   

"""
    Half adder: add two 1-bit binary numbers, ignoring carry input.

    Parameters:
        a, b: each is 0 or 1

    Returns:
        (sum_bit, carry_out): sum bit and carry

    Circuit implementation:
        sum_bit = a XOR b (XOR gate)
        carry_out = a AND b (AND gate)
    """

    sum_bit

=

a ^ b      

XOR: 0 when equal, 1 when different

    carry_out

=

a & b    

AND: carry only when both are 1

   

return

sum_bit

,

carry_out

def

full_adder

(

a

,

b

,

carry_in

)

:

   

"""
    Full adder: add two 1-bit binary numbers, considering carry input.

    Parameters:
        a, b, carry_in: each is 0 or 1

    Returns:
        (sum_bit, carry_out): sum bit and carry

    Implementation trick — cascade two half adders:
        Step 1: half_adder(a, b) → (s1, c1)
        Step 2: half_adder(s1, carry_in) → (sum_bit, c2)
        Final carry: carry_out = c1 OR c2
    """

    s1

,

c1

=

half_adder

(

a

,

b

)

    sum_bit

,

c2

=

half_adder

(

s1

,

carry_in

)

    carry_out

=

c1 | c2  

OR gate: if either has a carry, the final carry is present

   

return

sum_bit

,

carry_out

def

adder_4bit

(

a_bits

,

b_bits

)

:

   

"""
    4-bit ripple-carry adder: 4 full adders cascaded.

    Parameters:
        a_bits: 4-bit binary list, a_bits[0] is the lowest bit (LSB)
        b_bits: 4-bit binary list, b_bits[0] is the lowest bit (LSB)

    Returns:
        (sum_bits, final_carry): 4-bit sum (low bit first) and final carry

    Print the full carry-propagation process to show how ripple carry works.
    """

    carry_in

=

0

    sum_bits

=

[

]

   

print

(

"="

55

)

   

print

(

" 4-bit ripple-carry adder — carry propagation process"

)

   

print

(

"="

55

)

   

Display high bit first for readability

   

print

(

f

" Addend A: {list(reversed(a_bits))} (high bit first)"

)

   

print

(

f

" Addend B: {list(reversed(b_bits))} (high bit first)"

)

   

print

(

)

   

for

i

in

range

(

4

)

:

        s

,

carry_out

=

full_adder

(

a_bits

[

i

]

,

b_bits

[

i

]

,

carry_in

)

        sum_bits.

append

(

s

)

       

print

(

f

" [bit {i}] (the lowest bit is bit 0)"

)

       

print

(

f

" Input: A[{i}]={a_bits[i]}, B[{i}]={b_bits[i]}, carry-in={carry_in}"

)

       

print

(

f

" Output: sum bit={s}, carry-out={carry_out}"

)

       

if

carry_out:

           

print

(

f

" >> Carry generated! Carry={carry_out} will be passed to bit {i+1}"

)

        carry_in

=

carry_out  

The current carry becomes the carry-in of the next bit

       

print

(

)

   

print

(

f

" Result: {list(reversed(sum_bits))} (high bit first)"

)

   

print

(

f

" Final carry: {carry_in}"

)

   

print

(

"="

55

)

   

return

sum_bits

,

carry_in

def

decimal_to_4bit

(

n

)

:

   

"""Convert a decimal number 0-15 into a 4-bit binary list (low bit first)"""

   

return

[

(

n

i

)

&

1

for

i

in

range

(

4

)

]

def

bits_to_decimal

(

bits

)

:

   

"""Convert a binary bit list (low bit first) into a decimal integer"""

   

return

sum

(

bit *

(

2

** i

)

for

i

,

bit

in

enumerate

(

bits

)

)

============================================

Test cases

============================================

print

(

">>> runoob half-adder test <<<"

)

for

a

in

(

0

,

1

)

:

   

for

b

in

(

0

,

1

)

:

        s

,

c

=

half_adder

(

a

,

b

)

       

print

(

f

"  half_adder({a}, {b}) => sum={s}, carry={c}"

)

print

(

)

print

(

">>> runoob full-adder test <<<"

)

test_cases

=

[

   

(

0

,

0

,

0

)

,

(

0

,

0

,

1

)

,

(

0

,

1

,

0

)

,

(

0

,

1

,

1

)

,

   

(

1

,

0

,

0

)

,

(

1

,

0

,

1

)

,

(

1

,

1

,

0

)

,

(

1

,

1

,

1

)

]

for

a

,

b

,

cin

in

test_cases:

    s

,

c

=

full_adder

(

a

,

b

,

cin

)

   

print

(

f

"  full_adder({a}, {b}, carry_in={cin}) => sum={s}, carry={c}"

)

print

(

)

print

(

">>> runoob 4-bit adder test: 7 + 5 <<<"

)

a_dec

,

b_dec

=

7

,

5

a_bits

=

decimal_to_4bit

(

a_dec

)

b_bits

=

decimal_to_4bit

(

b_dec

)

result_bits

,

final_carry

=

adder_4bit

(

a_bits

,

b_bits

)

result_dec

=

bits_to_decimal

(

result_bits

)

print

(

f

" Expected: {a_dec} + {b_dec} = {a_dec + b_dec}"

)

print

(

f

" Result: {result_dec} (carry={final_carry})"

)

print

(

f

" runoob verification: {'Passed!' if result_dec == a_dec + b_dec else 'Failed!'}"

)

print

(

)

print

(

">>> runoob extra test: 10 + 6 (possible overflow) <<<"

)

a_bits2

=

decimal_to_4bit

(

10

)

b_bits2

=

decimal_to_4bit

(

6

)

result_bits2

,

carry2

=

adder_4bit

(

a_bits2

,

b_bits2

)

result_dec2

=

bits_to_decimal

(

result_bits2

)

full_result

=

result_dec2 +

(

carry2 *

16

)

print

(

f

" Expected: 10 + 6 = 16"

)

print

(

f

" 4-bit result: {result_dec2}, carry: {carry2}, full 5-bit value: {full_result}"

)

print

(

f

" runoob verification: {'Passed!' if full_result == 16 else 'Failed!'}"

)

Run the code and observe how the carry propagates between each bit--the lowest bit is computed first, generates a carry and passes it to the next bit, the next bit computes, generates a carry and passes it further on. This is where the name "ripple carry" comes from.


Interactive demo: 4-bit adder cascade animation (runoob carry propagation demo)

Below, a 4-bit ripple-carry adder is simulated with pure div+SVG. Click the buttons to step through how the carry propagates from bit 0 (lowest bit) to bit 3 (highest bit), or use "Auto Demo" to watch the whole process.

4-bit ripple-carry adder -- carry propagation animation

Addend A (high bit first)

Addend B (high bit first)

Full adder #3 (highest bit)

A=

0

B=

0

Carry-in Cin=

0

Sum Sum=

?

Carry-out Cout=

?

←

Full adder #2

A=

0

B=

0

Carry-in Cin=

0

Sum Sum=

?

Carry-out Cout=

?

←

Full adder #1

A=

0

B=

0

Carry-in Cin=

0

Sum Sum=

?

Carry-out Cout=

?

←

Full adder #0 (lowest bit)

A=

0

B=

0

Carry-in Cin=

0

Sum Sum=

?

Carry-out Cout=

?

Computation result:

Click "Next Step" to start the carry propagation demo