Extension
Warning
This material is not required by the GCSE.
It can sometimes be hard to see the point of logic gates at this level. It is true, though, that ALL operations carried out by the computer must be done by using logic gate circuits. Even something as simple as adding two numbers together!
Such an addition looks simple enough, but under the hood this operation has to be carried out using logic gates and associated Boolean operations - known as bit-wise operations.
This section looks at how this is done.
Binary addition
We'll start with two numbers we want to add, A
and B
in binary:
A
(3) =0011
B
(2) =0010
Bit-wise operations
We'll use the following Boolean operations:
AND
(logical AND, denoted by&
)XOR
(exclusive OR, denoted by^
)NOT
(logical NOT, denoted by~
)
Half Adder
A half adder is a simple circuit that adds two single-bit numbers. When we add two binary digits together we get the following:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0 Carry 1
So we need two outputs, a sum and a carry.
We can implement a half adder using Boolean operations as follows:
S
(sum) =A
XORB
C
(carry) =A
ANDB
As a truth table this is:
A | B | S (Sum) | C (Carry) |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
In this table:
- A and B are the input bits
- S (Sum) is the output sum bit
- C (Carry) is the output carry bit
The truth table shows that:
- When both A and B are 0, the sum is 0 and there's no carry
- When A is 0 and B is 1, the sum is 1 and there's no carry
- When A is 1 and B is 0, the sum is 1 and there's no carry
- When both A and B are 1, the sum is 0 and there's a carry of 1
Full Adder
To add together more than one binary digit we need to combine two half adders and a third input for the carry from the previous bit position.
The step by step process is:
-
Add the least significant bits (LSBs) of
A
andB
using a half adder:S0
=A0
XORB0
C0
=A0
ANDB0
-
Add the next bit positions of A and B using another half adder, considering the carry from the previous step:
S1
=A1
XORB1
XORC0
C1
= (A1
ANDB1
) OR (A1
ANDC0
) OR (B1
ANDC0
)
-
Repeat step 2 for each subsequent bit position until we reach the most significant bits (MSBs).
Example: Adding 3 and 2 using Boolean Operations
Let's add 3 and 2 using the full adder implementation:
A
(3) =0011
(binary)B
(2) =0010
(binary)
Add LSBs:
S0
=A0
XORB0
=1
XOR0
=1
C0
=A0
ANDB0
=1
AND0
=0
Add next bit positions:
S1
=A1
XORB1
XORC0
=0
XOR1
XOR0
=1
C1
= (A1
ANDB1
) OR (A1
ANDC0
) OR (B1
ANDC0
) = (0
AND1
) OR (0
AND0
) OR (1
AND0
) =0
-
Add next bit positions:
-
S2
=A2
XORB2
XORC1
=0
XOR0
XOR0
=0
C2
= (A2
ANDB2
) OR (A2
ANDC1
) OR (B2
ANDC1
) = (0
AND0
) OR (0
AND0
) OR (0
AND0
) =0
- The result is
S2 S1 S0
=0101
, which represents the sum 5 in binary.
While this process may seem cumbersome, it demonstrates how Boolean operations can be used to perform arithmetic operations like addition. In reality, digital circuits use more efficient implementations, such as carry-lookahead adders or parallel adders, to perform arithmetic operations.
Implementation using Python
Implementing binary addition using Boolean operations in Python can be an interesting exercise. Here's a possible implementation:
This implementation uses the same principles as the step-by-step process described earlier.
It converts the input integers to binary strings, iterates through each bit position, and uses Boolean operations to calculate the sum and carry. Finally, it converts the binary result back to an integer.
Keep in mind that this implementation is not optimized for performance and is intended for educational purposes only. In practice, you would use Python's built-in arithmetic operators for addition, which are much faster and more efficient.