Digital logic · Part 19 of 36

Binary numbers and logic gates

Intermediate 15 min

How voltages become bits, counting in binary and hex, and the seven basic gates with their truth tables.

What you will learn

  • You can convert between decimal, binary and hexadecimal.
  • You can write the truth tables of AND, OR, NOT, NAND, NOR, XOR and XNOR.
  • You can explain logic levels and why NAND is universal.

Before this: MOSFETs: voltage-controlled switches

From voltages to bits

A digital circuit only asks one question about a voltage: is it low (0) or high (1)? For a 3.3 V CMOS chip, inputs below about 0.8 V count as 0 and above about 2.0 V as 1 (exact thresholds are in the datasheet). The band in between is forbidden. Because small noise doesn't change a 0 into a 1, digital signals can be copied, stored and transmitted without degrading — the main reason the world went digital.

Eight toggle switches with LEDs lit or dark above them, like binary digits

Counting in binary

Binary uses powers of two: 1, 2, 4, 8, 16 … The number 1011₂ = 8 + 0 + 2 + 1 = 11. With n bits you can count from 0 to 2ⁿ − 1: 8 bits (a byte) give 0–255.

Long binary numbers are hard to read, so programmers group them in fours and write hexadecimal (0–9, A–F): 1011 0110₂ = B6₁₆ = 182. You've seen hex in colour codes (#2c75af) and memory addresses.

Negative numbers almost always use two's complement: invert all bits and add 1. In 8 bits, −1 is 1111 1111, and the range is −128 to +127. Addition works the same for positive and negative numbers — a single adder circuit does both.

The gates

GateOutput is 1 when…Symbol in expressions
NOTthe input is 0Ā or ¬A
ANDall inputs are 1A · B
ORat least one input is 1A + B
NANDnot all inputs are 1¬(A · B)
NORno input is 1¬(A + B)
XORan odd number of inputs is 1A ⊕ B
XNORan even number of inputs is 1 (for two: both equal)¬(A ⊕ B)

Truth table for two inputs:

ABANDORNANDNORXOR
0000110
0101101
1001101
1111000

NAND does everything

NAND (and NOR) are universal: any logic function can be built from NAND gates alone. NOT A = A NAND A; AND = NAND followed by NOT; OR = NOT A NAND NOT B (De Morgan, next lesson). In CMOS, NAND and NOR are also the cheapest gates — an inverting gate needs only four transistors for two inputs.

A row of eight small lamps, some on and some off, wired to toggle switches

A first useful circuit: the half adder

Adding two bits gives a sum bit and a carry bit:

  • Sum = A XOR B
  • Carry = A AND B

Chain full adders (which also take an incoming carry) and you add numbers of any width — the arithmetic unit of every processor starts here.

Real gates

Logic chips like the 74HC series (CMOS, 2–6 V) still sit on many hobby boards, but most logic today lives inside microcontrollers, FPGAs (expert tier) and custom chips. Unused CMOS inputs must never float: tie them to 0 or 1, or they pick up noise and can draw extra current.

Try it: Logic gate trainer

Toggle the inputs of a gate, watch the output, and fill in its truth table yourself.

Turn on JavaScript to use this lab. Worked example:

The XOR gate outputs 1 for inputs 01 and 10, and 0 for 00 and 11. Toggle the inputs with the buttons (or the keyboard) and fill in the truth table yourself; the lab checks your answers.

Key points

  • Digital circuits treat voltages as 0 or 1 with forbidden bands in between, so noise doesn't accumulate.
  • n bits count 0 to 2ⁿ − 1; hex groups bits in fours; two's complement represents negatives.
  • Seven basic gates: NOT, AND, OR, NAND, NOR, XOR, XNOR.
  • NAND and NOR are universal; a half adder is XOR (sum) + AND (carry).
  • Never leave CMOS inputs floating.

Flashcards

Question: What is 1101₂ in decimal?
Answer: 13.
Question: When is the output of a two-input XOR 1?
Answer: When the inputs differ.
Question: Why is NAND called universal?
Answer: Any logic function can be built from NAND gates alone.
Question: What does a half adder output?
Answer: Sum = A XOR B, carry = A AND B.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. What is 1011 0110₂ in decimal?

    Show the answer

    182

    128 + 32 + 16 + 4 + 2 = 182 (hex B6).

    Source: Harris & Harris, 1.4

  2. A two-input NOR gate has inputs A = 0, B = 0. Output?

    • 1
    • 0
    • undefined
    • depends on the supply
    Show the answer

    1

    NOR is 1 only when no input is 1.

    Source: Harris & Harris, 1.5

  3. In hexadecimal, the decimal number 255 is written ___.

    Show the answer

    FF

    255 = 15 × 16 + 15 = FF₁₆.

    Source: Harris & Harris, 1.4

  4. Which can be built from NAND gates alone?

    • NOT
    • AND
    • OR
    • Any logic function
    Show the answer

    NOT; AND; OR; Any logic function

    NAND is functionally complete.

    Source: MIT 6.004, L04

  5. What is the range maximum of an 8-bit two’s-complement number?

    Show the answer

    127

    8-bit two’s complement covers −128 to +127.

    Source: Harris & Harris, 1.4.6

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.