An adder is a simple circuit that adds two numbers. Half adders and full adders help computers do addition step by step.
Adder design (half adder, full adder) in Verilog
Introduction
Syntax
Verilog
module half_adder(input a, input b, output sum, output carry); assign sum = a ^ b; assign carry = a & b; endmodule module full_adder(input a, input b, input cin, output sum, output carry); wire sum1, carry1, carry2; half_adder ha1(a, b, sum1, carry1); half_adder ha2(sum1, cin, sum, carry2); assign carry = carry1 | carry2; endmodule
assign is used to connect logic expressions to outputs.
Half adder adds two bits; full adder adds three bits (including carry in).
Examples
Verilog
module half_adder(input a, input b, output sum, output carry); assign sum = a ^ b; // XOR for sum assign carry = a & b; // AND for carry endmodule
Verilog
module full_adder(input a, input b, input cin, output sum, output carry); wire sum1, carry1, carry2; half_adder ha1(a, b, sum1, carry1); half_adder ha2(sum1, cin, sum, carry2); assign carry = carry1 | carry2; endmodule
Sample Program
This test module checks all possible inputs for the full adder and prints the sum and carry results.
Verilog
module test_adder(); reg a, b, cin; wire sum, carry; full_adder fa(a, b, cin, sum, carry); initial begin $display("a b cin | sum carry"); a = 0; b = 0; cin = 0; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 0; b = 1; cin = 0; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 1; b = 0; cin = 0; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 1; b = 1; cin = 0; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 0; b = 0; cin = 1; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 0; b = 1; cin = 1; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 1; b = 0; cin = 1; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); a = 1; b = 1; cin = 1; #10 $display("%b %b %b | %b %b", a, b, cin, sum, carry); $finish; end endmodule
Important Notes
Half adder cannot handle carry input; full adder can.
Full adder is built using two half adders and an OR gate.
Use delays (#10) in testbench to allow signals to settle before displaying.
Summary
Half adder adds two bits and outputs sum and carry.
Full adder adds three bits (including carry in) and outputs sum and carry.
Full adder can be built by combining two half adders and an OR gate.
Practice
1. What does a half adder circuit do in digital logic?
easy
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]
Hint: Half adder adds exactly two bits only [OK]
Common Mistakes:
- Confusing half adder with full adder
- Thinking it multiplies bits
- Assuming it stores data
2. Which of the following is the correct Verilog module header for a half adder?
easy
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]
Hint: Inputs before outputs in module header [OK]
Common Mistakes:
- Swapping input and output keywords
- Wrong order of ports
- Using output as input
3. What is the output sum and carry when a full adder receives inputs a=1, b=1, carry_in=0?
medium
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]
Hint: Sum is XOR of all inputs, carry is majority logic [OK]
Common Mistakes:
- Forgetting carry_in in sum calculation
- Mixing AND and OR for carry
- Assuming sum equals carry
4. Identify the error in this Verilog full adder snippet:
assign sum = a ^ b ^ carry_in; assign carry = a & b & carry_in;
medium
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]
Hint: Carry = OR of AND pairs, not AND of all three [OK]
Common Mistakes:
- Using AND for carry instead of OR
- Confusing sum and carry assignments
- Ignoring carry_in in carry logic
5. How can a full adder be constructed using two half adders and one OR gate in Verilog?
hard
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]
Hint: Chain two half adders, OR their carries for full adder [OK]
Common Mistakes:
- Mixing inputs for half adders
- OR gate combining sums instead of carries
- Believing full adder can't be built from half adders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
