Boolean Logic and Gates
Logic gates are tiny circuits of a few transistors that compute on bits. AND, OR, and NOT, with their truth tables; NAND, NOR, and XOR; why AND, OR, and NOT can build any circuit, and so can NAND alone; turning a truth table into a circuit, using the equality circuit as the example; and the same operations on whole numbers at once with bash's bitwise operators &, |, ^, ~, and shifts, used for masks and permissions.
- 6 min
- 9 steps
- 2 questions
- Lesson 66 of 80
In this lesson
- From switches to logic
- AND, OR, NOT
- More gates
- Anything from a few
- From a truth table to a circuit
- Many bits at once
- Bitwise operators in bash
- Your turn
- So
Picking up where you left off.
From switches to logic
Module 1 said everything is bits. To compute with bits, a computer uses logic gates: tiny circuits, each made from a few transistors, that take one or two bits in and produce one bit out 1. A transistor is an electrically controlled switch; arranged in small groups, transistors implement each gate’s rule 1. Everything a processor does, adding, comparing, choosing, remembering, is built by wiring gates together, then wiring those circuits together, layer upon layer 1.
A gate’s behavior is fully described by its truth table: its output for every combination of inputs 1.
AND, OR, NOT
The three basic gates 1:
- AND outputs 1 only when both inputs are 1.
- OR outputs 1 when either input is 1 (or both).
- NOT has one input and flips it.
They’re the same and, or, and not you use in the shell’s &&, ||, and !, and in the logic of everyday rules: “the light comes on if it’s dark AND someone’s in the room.”
Quick check
Exclusive or: 1 when exactly one input is 1. Both 1 gives 0.
Anything from a few
AND, OR, and NOT together can build any circuit at all, and smaller sets work too: AND with NOT is enough, since A OR B equals NOT(NOT A AND NOT B) 1. In fact NAND alone can make all the others. NOT A is A NAND A; AND is NAND followed by NOT; OR is NAND of the two inputs’ NOTs. The next lesson’s Python program builds every gate, and then an adder, from a single nand function, which shows how little you need to start from.
From a truth table to a circuit
Designing a circuit follows a recipe 1:
- Write the truth table: every combination of inputs and the output you want.
- For each row where the output is 1, write an AND of the inputs that makes that row true.
- OR those together.
- Translate the expression into gates.
For a 1-bit equality circuit, A == B, the output is 1 in two rows: both 0, and both 1. That gives (NOT A AND NOT B) OR (A AND B): two NOTs, two ANDs, and an OR 1. (It’s the same as NOT (A XOR B), which is why XOR is the “different” gate.) Real designers then simplify, to use fewer gates and shorter paths 1.
Many bits at once
A gate works on single bits, but a processor applies the same gate to every bit of a number side by side: a 64-bit AND is 64 one-bit AND gates in a row 1. Programming languages expose exactly this as bitwise operators.
Bitwise operators in bash
bash’s arithmetic has them 2: & (AND), | (OR), ^ (XOR), ~ (NOT), and << and >> (shift left and right):
me@linuxbox:~$ echo $(( 12 & 10 )) $(( 12 | 10 )) $(( 12 ^ 10 ))
8 14 6
me@linuxbox:~$ echo $(( 2#1100 & 2#1010 ))
8
In binary: 1100 & 1010 is 1000 (8), 1100 | 1010 is 1110 (14), and 1100 ^ 1010 is 0110 (6), column by column.
Masks. AND with a pattern of 1s keeps just those bits and clears the rest:
me@linuxbox:~$ echo $(( 0xC8 & 0x0F ))
8
0x0F is 00001111, so this keeps the low four bits of 11001000: 1000, 8. File permissions work this way: the “others may write” bit is one bit in the mode, tested with a mask (Shell course, module 3).
Shifts. Shifting left by one doubles a number, and right by one halves it, dropping the remainder, just as moving digits in decimal multiplies or divides by ten:
me@linuxbox:~$ echo $(( 5 << 1 )) $(( 5 << 3 )) $(( 40 >> 2 ))
10 40 10
NOT flips every bit, all 64 of them in bash, so ~0 is all ones, which in two’s complement is −1 (module 1):
me@linuxbox:~$ echo $(( ~0 ))
-1
Quick check
echo $(( 0xC8 & 0x0F )) prints 8. What did the & 0x0F do?0x0F is 00001111; AND with it is a mask that keeps the low nibble, here 1000, which is 8.
Your turn
Exercises
- Write the truth table for NOR from memory, then check it against the diagram.
- Using only NAND, how do you make NOT? Write it as an expression.
- Work out
9 & 12,9 | 12, and9 ^ 12by hand in binary, then check in bash. echo $(( 0755 & 0007 )): what are the “others” permission bits of mode 755?- Why does
echo $(( 1 << 10 ))print 1024? - XOR a number with the same value twice:
echo $(( (200 ^ 77) ^ 77 )). What happens, and why?
Answers
- 1 only for inputs 0 0; otherwise 0.
- NOT A = A NAND A.
1001 & 1100 = 1000(8),1001 | 1100 = 1101(13),1001 ^ 1100 = 0101(5).- 5: read and execute (
101). A leading 0 makes bash read0755as octal 2. - Shifting 1 left by 10 places is 2¹⁰.
- You get 200 back: XOR with the same value twice cancels out, which is why XOR turns up in simple encryption and checksums.
So
Logic gates, built from a few transistors each, compute on bits: AND (both), OR (either), NOT (flip), and NAND, NOR, and XOR (exactly one). AND, OR, and NOT can build any circuit, and NAND alone can too. A truth table becomes a circuit by ANDing the inputs for each row that outputs 1 and ORing the rows together. Processors apply gates to every bit of a number at once, and bash exposes that as &, |, ^, ~, <<, and >>: masks keep chosen bits, shifts multiply and divide by two.
Lesson complete
Nice work.
Sources for this lesson
- 1Suzanne J. Matthews, Tia Newhall, Kevin C. Webb. Dive into Systems. No Starch Press (free online edition). 2022. verifiedCh. 4 Binary and Data Representation: bits as two voltage states, bytes (8 bits, 256 values, smallest addressable unit), words of 32 or 64 bits, n bits give 2^n values; decimal and binary place value with 0b and 0x prefixes; hexadecimal as four bits per digit; fixed storage sizes and unsigned ranges; two's complement with a negative-weighted top bit, one zero, range -2^(n-1) to 2^(n-1)-1, all ones is -1, negation by flipping bits and adding one; subtraction as adding the negation, reusing negation and addition circuits; overflow and the odometer analogy.
- 2Shell Arithmetic (Bash Reference Manual). Free Software Foundation. verifiedEvaluation is done in the largest fixed-width integers available, with no check for overflow; division by zero is trapped; operators as in C; integer constants may be written base#n with base 2 to 64, and 0x for hex, a leading 0 for octal.