Computing and the Command Line

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

  1. From switches to logic
  2. AND, OR, NOT
  3. More gates
  4. Anything from a few
  5. From a truth table to a circuit
  6. Many bits at once
  7. Bitwise operators in bash
  8. Your turn
  9. So

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.

Six panels, each with a gate symbol and truth table. AND, 1 when both are 1: 00 gives 0, 01 gives 0, 10 gives 0, 11 gives 1. OR, 1 when either is 1: 0, 1, 1, 1. NOT, flips its one input: 0 gives 1, 1 gives 0. NAND, NOT of AND: 1, 1, 1, 0. NOR, NOT of OR: 1, 0, 0, 0. XOR, 1 when exactly one is 1: 0, 1, 1, 0. Bottom, a terminal: echo $(( 12 & 10 )) $(( 12 | 10 )) $(( 12 ^ 10 )) prints 8 14 6; in binary 1100 & 1010 = 1000, | gives 1110, ^ gives 0110.
Six gates; AND, OR, and NOT can build any circuit, and so can NAND alone. Credit: StudyCorner diagram · CC BY 4.0 · Source

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.”

More gates

Three more come up constantly 1:

  • NAND: NOT of AND. 0 only when both inputs are 1.
  • NOR: NOT of OR. 1 only when both inputs are 0.
  • XOR, exclusive or: 1 when exactly one input is 1. It answers “are these two bits different?”

In diagrams, the small circle on NAND and NOR means “then NOT” 1.

Quick check

What does XOR output for inputs 1 and 1?

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:

  1. Write the truth table: every combination of inputs and the output you want.
  2. For each row where the output is 1, write an AND of the inputs that makes that row true.
  3. OR those together.
  4. 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?

Your turn

Exercises

  1. Write the truth table for NOR from memory, then check it against the diagram.
  2. Using only NAND, how do you make NOT? Write it as an expression.
  3. Work out 9 & 12, 9 | 12, and 9 ^ 12 by hand in binary, then check in bash.
  4. echo $(( 0755 & 0007 )): what are the “others” permission bits of mode 755?
  5. Why does echo $(( 1 << 10 )) print 1024?
  6. XOR a number with the same value twice: echo $(( (200 ^ 77) ^ 77 )). What happens, and why?
Answers
  1. 1 only for inputs 0 0; otherwise 0.
  2. NOT A = A NAND A.
  3. 1001 & 1100 = 1000 (8), 1001 | 1100 = 1101 (13), 1001 ^ 1100 = 0101 (5).
  4. 5: read and execute (101). A leading 0 makes bash read 0755 as octal 2.
  5. Shifting 1 left by 10 places is 2¹⁰.
  6. 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.

1day streak
0/1today's goal
–correct

Up next · 7 min

Adding with Gates

Next lesson
Sources for this lesson
  1. 1
    Suzanne 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.
  2. 2
    Shell 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.