Adder design (half adder, full adder) in Verilog - Time & Space Complexity
We want to understand how the time to add numbers grows as the size of the numbers increases.
How does the design of adders affect the time it takes to compute sums?
Analyze the time complexity of this simple ripple carry adder design.
module full_adder(
input a, b, cin,
output sum, cout
);
assign sum = a ^ b ^ cin;
assign cout = (a & b) | (b & cin) | (a & cin);
endmodule
module ripple_carry_adder(
input [3:0] A, B,
output [3:0] SUM,
output COUT
);
wire c1, c2, c3;
full_adder fa0(A[0], B[0], 1'b0, SUM[0], c1);
full_adder fa1(A[1], B[1], c1, SUM[1], c2);
full_adder fa2(A[2], B[2], c2, SUM[2], c3);
full_adder fa3(A[3], B[3], c3, SUM[3], COUT);
endmodule
This code adds two 4-bit numbers using a chain of full adders where each carry depends on the previous one.
Look at the chain of full adders that process each bit.
- Primary operation: Each full adder computes sum and carry for one bit.
- How many times: Once per bit, so 4 times for 4 bits.
Each bit addition waits for the carry from the previous bit, so the time grows with the number of bits.
| Input Size (n bits) | Approx. Operations (carry steps) |
|---|---|
| 4 | 4 |
| 8 | 8 |
| 16 | 16 |
Pattern observation: The time to get the final carry grows linearly as the number of bits increases.
Time Complexity: O(n)
This means the time to add two numbers grows directly with the number of bits in the numbers.
[X] Wrong: "All bits are added at the same time, so addition is constant time."
[OK] Correct: Each bit's carry depends on the previous bit's carry, so the addition must wait step-by-step.
Understanding how addition time grows helps you design faster circuits and shows you can think about how hardware speed depends on design choices.
"What if we used a carry lookahead adder instead of a ripple carry adder? How would the time complexity change?"
Practice
Solution
Step 1: Understand half adder inputs and outputs
A half adder takes two bits as input and produces two outputs: sum and carry.Step 2: Compare with other options
Full adder adds three bits, multiplication and storage are unrelated to half adder.Final Answer:
Adds two single bits and produces a sum and carry output -> Option CQuick Check:
Half adder = two bits sum and carry [OK]
- Confusing half adder with full adder
- Thinking it multiplies bits
- Assuming it stores data
Solution
Step 1: Identify correct input and output declaration order
Inputs come first, then outputs. Inputs are 'a' and 'b', outputs are 'sum' and 'carry'.Step 2: Check each option for correct syntax
module half_adder(input a, input b, output sum, output carry); correctly declares inputs and outputs. Others mix input/output or order incorrectly.Final Answer:
module half_adder(input a, input b, output sum, output carry); -> Option BQuick Check:
Inputs first, outputs next in module header [OK]
- Swapping input and output keywords
- Wrong order of ports
- Using output as input
Solution
Step 1: Calculate sum for inputs a=1, b=1, carry_in=0
Sum = a XOR b XOR carry_in = 1 XOR 1 XOR 0 = 0.Step 2: Calculate carry output
Carry = (a AND b) OR (b AND carry_in) OR (a AND carry_in) = (1 AND 1) OR (1 AND 0) OR (1 AND 0) = 1.Final Answer:
sum=0, carry=1 -> Option DQuick Check:
Full adder sum and carry for 1,1,0 = 0,1 [OK]
- Forgetting carry_in in sum calculation
- Mixing AND and OR for carry
- Assuming sum equals carry
assign sum = a ^ b ^ carry_in; assign carry = a & b & carry_in;
Solution
Step 1: Review carry logic in full adder
Carry = (a & b) | (b & carry_in) | (a & carry_in), not a & b & carry_in.Step 2: Compare given code with correct logic
Given code uses AND of all inputs, which misses cases where two inputs are 1 but third is 0.Final Answer:
Carry logic should use OR of AND terms, not AND of all inputs -> Option AQuick Check:
Full adder carry = OR of AND pairs [OK]
- Using AND for carry instead of OR
- Confusing sum and carry assignments
- Ignoring carry_in in carry logic
Solution
Step 1: Understand half adder roles in full adder
First half adder adds inputs a and b producing sum1 and carry1.Step 2: Use second half adder and OR gate
Second half adder adds sum1 and carry_in producing final sum and carry2; OR gate combines carry1 and carry2 for final carry.Final Answer:
First half adder adds a and b, second adds sum and carry_in; OR gate combines carries -> Option AQuick Check:
Full adder = 2 half adders + OR gate [OK]
- Mixing inputs for half adders
- OR gate combining sums instead of carries
- Believing full adder can't be built from half adders
Ready to prove it?
Pass the test and this pattern moves unknown
