How does a computer add two numbers together?
14 x 16 (224)
+ 11 x 1
384 – 322 BC
Laid the groundwork for a system of logic based on binary propositions, true and false.
1815 – 1864
In 1854 introduced Boolean algebra in his book "An Investigation of the Laws of Thought," which presented a symbolic representation of Aristotle’s logic.
While elementary algebra deals with numerical operations, Boolean algebra deals with logical operations. These operations form the backbone of programming logic and the design of digital circuits.
Each of these operations can be represented by grouping the primary operations together:
There are no numbers in Boolean algebra, only symbols for true and false values.
To visualize and understand the outcome of these operations, we use Truth tables. These tables enumerate all possible combinations of input truth values and their corresponding output.
A | B | OUT |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
A | B | OUT |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
A | B | OUT |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
A | B | OUT |
---|---|---|
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 0 |
1 | 1 | 1 |
If A then B otherwise true, i.e. a conditional statement.
A | B | OUT |
---|---|---|
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
An OR operation represents addition:
1 + 1 = 1
1 + 0 = 1
0 + 0 = 0
0 + 1 = 1
An AND operation represents multiplication:
1 * 1 = 1
1 * 0 = 0
0 * 0 = 0
0 * 1 = 0
Subtraction and division do not exist in Boolean algebra.
Boolean operations serve as the building blocks for logical circuits that process and compute information in our digital devices.
1916 – 2001
Often regarded as one of the most influential yet overlooked figures in computer science, Claude Shannon laid the foundation for the Information Age with his groundbreaking work in Information Theory.
Every single concept from Boole’s algebra had its physical counterpart in an electric circuit. An on switch could stand for "true" and an off switch for "false," and the whole thing could be represented in 1’s and 0’s. More important, as Shannon pointed out, the logical operators of Boole’s system—AND, OR, NOT—could be replicated exactly as circuits.
By merging principles of Boolean algebra and binary representation with electronic circuits, Shannon forged a path that revolutionized computing.
These early devices used proportional differences between gears to perform calculations. They were limited to specific tasks and lacked the versatility of general-purpose computations.
Mechanical calculator
Digital computers perform calculations using electronic switches to implement logical circuits.
ENIAC programmers
The types of switches used to perform boolean operations in digital computers changed as technology developed.
Relay
Vacuum tube
Transitors can be combined in certain ways to create logic gates. These gates execute basic logical functions that are crucial in performing complex computational tasks. The most common logic gates include:
NOT Gate: Flips the input (1 becomes 0 and vice versa).
AND Gate: Outputs 1 only when both inputs are 1.
OR Gate: Outputs 1 if any input is 1.
XOR Gate: Outputs 1 if only one input is 1.
By chaining together different logic gates, we can create logic circuits. These circuits can perform more complex operations and calculations.
Performing binary arithmetic using boolean logic:
In binary, 2 bits are required to represent the number 2. The extra bit is "carried" to the left. The rightmost bit is reset to 0 because of the carry.
A | B | A+B |
---|---|---|
0 | 0 | 00 |
0 | 1 | 01 |
1 | 0 | 01 |
1 | 1 | 10 |
A | B | AND | XOR |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 0 | 0 | 1 |
1 | 1 | 1 | 0 |
Notice the leftmost bit represents the result of an AND operation. The rightmost bit is the result of an XOR operation.
Adds two single binary digits and returns a sum plus a carry value.
(carry)
(sum)
A full adder is a combination of two half-adders with an additional OR gate to manage the carry bit.
(carry)
(carry)
(sum)
Move from right to left and carry a 1 if we add two 1s together.
A | 1 | 1 | 0 | 1 | 1 | (27) |
B | 0 | 1 | 1 | 1 | 1 | (15) |
1
42
0
1
1
1
1
0
1
1
0
1
Full-adders can be chained together to add large numbers
FA | FA | FA | FA | FA | FA | ||
---|---|---|---|---|---|---|---|
C | 1 | 1 | 1 | 1 | 1 | 0 | |
A | 0 | 1 | 1 | 0 | 1 | 1 | (27) |
B | 0 | 0 | 1 | 1 | 1 | 1 | (15) |
1 | 0 | 1 | 0 | 1 | 0 | 42 |
Bitwise means evaluating a series of bits, on order, from right to left. Bitwise operators apply the operation to each set of digits from right to left.
In Ruby, bitwise operations are represented as:
AND | & |
OR | | |
XOR | ^ |
NOT | ~ |
Left shift | << |
Right shift | >> |
The AND operation 13 & 11 = 9 can be visualized as:
1 1 0 1 (13)
& 1 0 1 1 (11)
———————
1 0 0 1 (9)
Shifting the bits to the left is multiplying by 2. Shifting to the right is dividing by two.
7 << 2 = 28
000111 << 2 = 011100
40 >> 2 = 10
0101000 >> 2 = 0001010