In our previous lessons, we looked at how individual gates like NAND and XOR work. We also learned how to count in binary. Now, we are going to combine those two ideas.
Think of logic gates as the building blocks of a brain. On their own, they just respond to high or low voltages. But when we wire them together in specific ways, they can perform math just like a calculator. In this lesson, we will build a circuit that can add numbers together. This is the first step toward building a central processing unit (CPU).
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Before we build the circuit, we need to remember the four basic rules of adding single bits. In binary, addition looks very similar to the math you learned in school, but it is much simpler because there are only two digits: 0 and 1.
Operation
Result
Sum (S)
Carry (C)
0+0
0
0
0
0+1
1
1
0
1+0
1
1
0
1+1
10
0
1
Notice what happens when we add 1+1. The result is 10 in binary (which is 2 in decimal). Just like in decimal math where 5+5=10 results in a 0 in the current column and a 1 carried to the next, binary addition uses a Sum bit and a Carry bit.
To turn these rules into a circuit, we need two outputs. Let's look at the Sum (S) column in the table above. The Sum is 1 only when the inputs are different (0+1 or 1+0). This is exactly how an XOR gate behaves!
Now look at the Carry (C) column. The Carry is only 1 when both inputs are 1. This is the behavior of an AND gate.
A circuit that uses one XOR gate for the Sum and one AND gate for the Carry is called a Half Adder.
It works perfectly for adding two bits, but it has one problem: it cannot accept a carry from a previous math problem. To solve that, we need a Full Adder.
In real computers, we add long strings of numbers. When you add the second column of a math problem, you often have to include a carry from the first column.
A Full Adder handles three inputs instead of two:
Input A: The first bit.
Input B: The second bit.
Carry In (Cin): The carry bit from the previous addition.
By adding this third input, we can chain many adders together to add very large numbers. The logic gets slightly more complex, but the goal is the same: produce one Sum output and one Carry Out (Cout) output.
To build a Full Adder, we combine two stages of logic. Let's look at how the circuit you will work with is organized:
1. Finding the Sum (S)
The Sum is calculated by checking if the total number of 1 inputs is odd. We use two XOR gates for this.
The first XOR gate compares A and B.
The second XOR gate compares that result with Cin.
If only one input is high, or if all three are high, the Sum output (S) will be 1.
2. Finding the Carry Out (Cout)
We need a carry if at least two of the inputs are 1. In your upcoming exercise, this is handled using NAND gates. While we could use AND and OR gates, NAND gates are often preferred in manufacturing because they are faster and use fewer transistors.
The circuit checks if (A and B) are both 1.
It also checks if one of (A or B) is 1 AND the Cin is 1.
If either of those cases is true, the Carry Out becomes 1.
For example, it will look something like this:
In this lesson, we moved from simple logic gates to a functional math circuit. You learned that:
Binary addition follows four simple rules.
An XOR gate is the primary tool for calculating a Sum.
A Full Adder uses a Carry In (Cin) to allow for multi-bit addition.
Now it is time to put this into practice. In the next section, you will go into the CodeSignal Logic simulator to wire these gates together. You will follow a provided design to create a working 1-bit Full Adder that takes three inputs and produces the correct Sum and Carry bits. Pay close attention to how the signals flow from the inputs through the XOR and NAND gates to reach the final outputs!