Digital Logic

Gates and truth tables, logic families and levels, adders, multiplexers, flip-flops, registers and counters, timing, and interfacing logic chips to a microcontroller.

Digital circuits work with two voltage levels, called 0 (low) and 1 (high), and combine them with gates to make decisions, count, store and move data. Everything from a microcontroller to a computer is built from them. Even if you only use microcontrollers, a few logic chips (a level shifter, a shift register, a decoder, a gate that debounces) turn up in nearly every project, and the same ideas explain how a PLC’s ladder logic and a processor’s instructions work.

Binary and hexadecimal

A bit is one binary digit. Four bits make a nibble (16 values, one hexadecimal digit), eight make a byte (256 values), and so on: n bits give 2n values. Hexadecimal (base 16, digits 0–9 and A–F) is a compact way to write binary: each hex digit is exactly four bits.

DecimalBinaryHexNotes
000000
501015
910019Largest BCD digit
101010A
151111FA full nibble
1831011 0111B7Each hex digit is one nibble: B = 1011, 7 = 0111
2551111 1111FFA full byte

Convert 0xB7 to decimal: B × 16 + 7 = 11 × 16 + 7 = 183. The site’s Bitwise and Register Field Calculator converts and manipulates values for you. A signed byte uses two’s complement: the top bit has weight −128, so 0xFF is −1 (see Networking Fundamentals).

Gates and truth tables

A gate is a circuit that gives one output from one or more inputs according to a rule. A truth table lists the output for every combination of inputs: n inputs give 2n rows. Six gates cover almost everything.

ANDY = A · BA B Y0 0 00 1 01 0 01 1 1ORY = A + BA B Y0 0 00 1 11 0 11 1 1NOTY = A̅ (inverter)A Y0 11 0Output is the opposite of the input.NANDY = (A · B)̅A B Y0 0 10 1 11 0 11 1 0NORY = (A + B)̅A B Y0 0 10 1 01 0 01 1 0XORY = A ⊕ BA B Y0 0 00 1 11 0 11 1 0
The six basic gates with their symbols, Boolean expressions and truth tables.
GateOutput is 1 when…Typical use
ANDall inputs are 1Enabling: a signal passes only when a second input allows it
ORany input is 1Combining alarms or start conditions
NOT (inverter)the input is 0Reversing a level; buffering
NANDany input is 0 (not all are 1)The universal gate: every other gate can be built from NANDs
NORall inputs are 0Also universal; used in latches
XORthe inputs differAdders, parity, comparing two bits
  • Relay and PLC logic is the same. Contacts in series are an AND, contacts in parallel are an OR, a normally-closed contact is a NOT: see Ladder Logic in Practice.
  • Open-drain and open-collector outputs can only pull low; several can share one line (a wired-AND) with a pull-up resistor, as in I2C.
  • Schmitt-trigger inputs (74HC14, 74HC132) have two thresholds, so a slow or noisy signal gives a clean output.

Boolean algebra and De Morgan

RuleExpressionUse
Identity and nullA · 1 = A; A · 0 = 0; A + 0 = A; A + 1 = 1Simplify when an input is fixed
ComplementA · A̅ = 0; A + A̅ = 1A signal and its inverse
DistributiveA · (B + C) = A · B + A · CFactor and expand
AbsorptionA + A · B = ADrop redundant terms
De Morgan(A · B)̅ = A̅ + B̅; (A + B)̅ = A̅ · B̅Convert AND to OR with inversions; build any function from NANDs

To simplify a function, write its truth table, then group the 1s in a Karnaugh map or apply the rules above. Fewer gates mean less cost and delay; but clear logic is worth more than the last gate, and a programmable device (PLD, FPGA) or a few lines of code can replace a handful of gates.

Logic families and levels

Gates come in families that differ in supply voltage, speed, power and the voltages they treat as 0 and 1. The 74-series numbering stays the same (7400 is four NAND gates), with letters for the technology: LS (old bipolar TTL), HC (fast CMOS), HCT (CMOS with TTL-compatible inputs), LVC and AHC (low-voltage CMOS), and the 4000 series (slow CMOS with a wide supply range, 3–15 V).

FamilySupplyVIH minVIL maxVOH minVOL maxHigh noise marginLow noise margin
LS TTL5.0 V2.00 V0.80 V2.70 V0.50 V0.70 V0.30 V
74HC (5 V)5.0 V3.50 V1.50 V4.40 V0.33 V0.90 V1.17 V
74HCT (5 V)5.0 V2.00 V0.80 V4.40 V0.33 V2.40 V0.47 V
74LVC (3.3 V)3.3 V2.00 V0.80 V2.20 V0.55 V0.20 V0.25 V

The noise margin is how much noise a signal can have and still be read correctly: the high margin is VOH − VIH, the low margin is VIL − VOL. An LS TTL gate driving another has margins of only 0.7 V and 0.3 V, a CMOS gate has more than 0.9 V and 1.1 V: another reason CMOS dominates.

Driver → receiverWorks?Notes
3.3 V LVC → 5 V HCTYesHCT’s VIH is 2.0 V, so 3.3 V is read as high
3.3 V → 5 V HCNo (marginal)HC needs 3.5 V for a high at 5 V
5 V HC or HCT → 3.3 V LVCYes, if the 3.3 V input is 5 V tolerantLVC inputs are 5 V tolerant; many MCU pins are not
5 V → 3.3 V MCU pinOnly with a divider or level shifterSee Microcontroller Electronics
  • Unused inputs must be tied to 0 or 1 (a CMOS input floats and oscillates, drawing current).
  • Outputs must not be connected together unless they are open-drain or tri-state types.
  • Fan-out is how many inputs an output can drive: a CMOS input needs almost no DC current, so the limit is the capacitance (it slows the edges), typically a few tens of inputs.
  • Decouple every chip with 100 nF at its supply pins (see Capacitors).

Combinational circuits: decoders, multiplexers and adders

A combinational circuit has no memory: its output depends only on its present inputs. The common building blocks are decoders, multiplexers and adders.

BlockFunctionExample chipUse
DecoderN select lines turn on one of 2N outputs74HC138 (3 to 8), 74HC154 (4 to 16)Chip-select lines; driving one of many LEDs or relays
EncoderThe opposite: which of 2N inputs is active, as a binary number74HC148 (priority encoder)Keypads, interrupt priority
MultiplexerSelects one of 2N inputs to one output74HC151 (8:1), 74HC4051 (analog 8:1)Reading many sensors with one ADC pin
DemultiplexerSends one input to one of 2N outputs74HC138 used backwardsRouting signals
ComparatorCompares two binary numbers74HC85Thresholds in hardware
AdderAdds two binary numbers74HC283 (4 bit)Arithmetic units
Tri-state bufferPasses, or disconnects (high impedance)74HC244, 74HC245Shared buses

Worked example: adding 5 and 3 in binary

  • A half adder adds two bits: sum = A XOR B, carry = A AND B. A full adder also takes a carry in: sum = A XOR B XOR Cin, carry out = A · B + Cin · (A XOR B).
  • Add 5 (0101) and 3 (0011) one bit at a time from the right: bit 0: 1 + 1 = 0 carry 1; bit 1: 0 + 1 + carry 1 = 0 carry 1; bit 2: 1 + 0 + carry 1 = 0 carry 1; bit 3: 0 + 0 + carry 1 = 1 carry 0.
  • The result is 1000 = 8. Each stage waits for the carry of the previous one (a ripple-carry adder), which is why a wide adder is slower; fast adders look ahead.
  • Multiplexer sizing: an 8-input multiplexer needs 3 select lines (23 = 8); a memory with 16 address lines holds 216 = 65 536 bytes (64 KB).

Latches, flip-flops and registers

To remember a bit, a circuit needs feedback. The simplest memory is an SR latch of two cross-coupled NOR (or NAND) gates: one input sets the output to 1, the other resets it to 0, and with both inactive it holds. A flip-flop adds a clock so that the state changes only at a defined instant.

ElementBehaviorUse
SR latchSet and reset inputs; holds when both are inactiveSwitch debounce, simple memory, alarms
D latchFollows D while the enable is high; holds when lowHolding data on a bus
D flip-flopCopies D to Q on the rising edge of the clockThe basic register bit; synchronizing an input
T flip-flopToggles on each clock when T = 1Counters, divide by two
JK flip-flopSet, reset, hold or toggleGeneral-purpose; rarely used now
RegisterA row of D flip-flops sharing a clockHolding a byte (74HC574, 74HC273)
Shift registerData moves one place on each clockSerial to parallel and the reverse (74HC595, 74HC165)
  • Edge-triggered flip-flops change only at the clock edge, which makes large synchronous systems predictable.
  • Asynchronous set and reset inputs force the state at once and bypass the clock: use them to initialize at power-up.
  • Metastability. A signal that changes close to the clock edge (an external button, another clock domain) can put a flip-flop in an undefined state for a moment; pass it through two flip-flops in series to resolve it (a synchronizer).
  • Power-up state of flip-flops is random: add a reset.

Counters and frequency division

A counter is a chain of flip-flops that counts clock pulses. In a ripple (asynchronous) counter each stage clocks the next, so each stage divides the frequency by two; in a synchronous counter all stages share the clock and change together, which avoids glitches and runs faster.

3-bit binary counter: each stage divides the frequency by two00001001201030114100510161107111CLKQ0 (1)Q1 (2)Q2 (4)Count 5 = 101: Q2 = 1, Q1 = 0, Q0 = 1. After 7 (111) the next clock returns every output to 0 (modulo 8).
A 3-bit binary counter: each output toggles at half the frequency of the one before.
CounterSequenceChipUse
Binary0 to 2n − 1, then back to 074HC161 (4 bit), 74HC4040 (12 bit ripple)Frequency division, addresses
Decade (BCD)0 to 974HC160, 74HC4017 (decade with 10 decoded outputs)Counting in decimal; LED chasers
Up/downCounts either way74HC191Position counting from an encoder
Johnson (twisted-ring)A moving block of 1s74HC4017Sequencers

Worked example: making 1 Hz from a 1 MHz clock

  • The division needed is 1 000 000 ÷ 1 = 1 000 000. Binary stages divide by powers of two: 20 stages divide by 220 = 1 048 576, giving 0.954 Hz, 4.6 percent slow: no good for a clock.
  • Using decade counters, each dividing by 10, 6 of them divide by 106 = 1 000 000 exactly: 1 MHz → 100 kHz → 10 kHz → 1 kHz → 100 Hz → 10 Hz → 1 Hz.
  • This is how a clock keeps time: a 32 768 Hz crystal divided by 215 gives exactly 1 Hz, so 15 binary stages are the right choice for it.
  • In a microcontroller, the same job is done by a timer with a prescaler and a compare value (see Microcontroller Electronics).

Timing: delay, setup, hold and clock speed

Signals take time to pass through gates. The propagation delay tpd is the time from an input change to the output change (about 5–15 ns for 74HC at 5 V, 2–5 ns for LVC). A flip-flop needs its data stable for a setup time tsu before the clock edge and a hold time th after it; the output changes a clock-to-Q delay tcq after the edge.

Worked example: how fast can this circuit be clocked?

  • A flip-flop output feeds three gates in series (24 ns in total at 8 ns each) and then the next flip-flop. The clock period must cover tcq + logic + tsu = 12 + 24 + 5 = 41 ns.
  • The maximum clock frequency is 1 ÷ 41 ns = 24.4 MHz. Allow a margin of at least 20 percent for temperature, supply and part variation, so use about 20 MHz.
  • Cutting the longest path (a faster family, fewer gates, or splitting the logic with a register: pipelining) raises the speed.
  • Hold violations occur when the data changes too soon after the clock edge: they cannot be cured by slowing the clock, only by adding delay to the data path.
  • Glitches. When several inputs change at once, a combinational output can flicker briefly; do not use it as a clock, and use synchronous design where you can.
  • Clock distribution. A clock that reaches different chips at different times (skew) causes errors: keep the clock traces short and equal on fast boards.
  • Switching noise. Fast edges draw spikes of current: decouple every chip and add series resistors (22–47 Ω) on long fast lines.

Using logic chips in practice

JobPartNotes
Level-shift or buffer a signal74HC244/245, 74LVC1T45, 74AHCT125Check the direction control and the speed
Clean a slow or noisy edge74HC14 Schmitt inverterDebounce with an RC (see Microcontroller Electronics)
Enable one chip of several74HC138 decoderAddress decoding
Add outputs to a microcontroller74HC595 shift registerThree pins control any number of outputs
Add inputs74HC165 shift registerReads a bank of switches
Combine a few signalsSingle-gate chips (74LVC1G08)One gate in a tiny package
Drive a loadULN2803 (Darlington array), 74HC with a MOSFETSee Driving Motors and Actuators
Count or divide74HC4040, 74HC4017The CMOS 4000 series works from 3 to 15 V

Logic chips with a microcontroller

Worked example: 16 outputs from three pins

  • A 74HC595 gives 8 outputs for three microcontroller pins (data, clock, latch). Chaining two of them gives 16 outputs, still on three pins; each extra chip adds 8.
  • Shifting 16 bits at a 10 MHz SPI clock takes 16 ÷ 10 MHz = 1.6 µs, then a pulse on the latch pin moves them to the outputs together, so nothing glitches while shifting.
  • Multiplexing LEDs: lighting one of 8 rows at a time, each LED is on only 1/8 of the time. To keep an average of 5 mA each, the peak current must be 8 × 5 = 40 mA: check the LED’s peak rating and the driver’s limit, and refresh above 100 Hz to avoid flicker.
  • A 74HC595 outputs about 6 mA per pin and 70 mA in total: use transistors or a driver chip (TPIC6B595, ULN2003) for LEDs and relays.
  • SPI is a shift register. The 74HC595 and 74HC165 connect directly to a microcontroller’s SPI pins.
  • Wired-AND buses and interrupts: several open-drain devices can share an interrupt line with one pull-up.
  • Glue logic. A gate or two can combine chip-select and enable signals so that the microcontroller does not need extra pins.
  • Use the microcontroller instead when the function is complex: software is cheaper than hardware, until speed or simultaneity demands logic.

Memory and programmable logic

DeviceWhat it isUse
ROM, EPROM, flashNon-volatile memory, read (and rewritten slowly)Programs and constants
SRAMFast volatile memory, a flip-flop per bitWorking memory, caches
DRAMDense volatile memory refreshed regularlyLarge memories (PCs, boards with SDRAM)
EEPROM, FRAMSmall non-volatile memory with byte writesSettings, calibration data
PLD (GAL, CPLD)Programmable AND/OR logic arrays with flip-flopsReplace several chips of glue logic
FPGAThousands to millions of programmable gates and flip-flops, configured by a bitstreamFast parallel processing, custom interfaces, prototyping chips
MicrocontrollerA processor with memory and peripheralsMost control tasks

A memory with n address lines has 2n locations: 16 lines address 65 536 locations. Programmable logic is described in a hardware description language (VHDL or Verilog) or drawn as a schematic, compiled to a configuration file, and loaded into the chip. Ladder logic on a PLC is a software version of the same idea: see How PLCs Work.

Common faults

SymptomLikely causeTry
Output stuck high or lowMissing supply or ground, an input driven by nothing, outputs connected togetherCheck the supply pins; scope the inputs; look for two drivers on a line
Chip hot or the supply current highA floating CMOS input, two outputs fighting, a shortTie unused inputs; check for contention
Random or intermittent behaviorA floating input, no decoupling, noise on a clock or resetAdd pull-ups, 100 nF capacitors, a Schmitt input
Wrong value read from the other voltage familyLogic levels not compatibleCheck VOH against VIH; use a level shifter or an HCT part
Counter skips or counts extraBounce or noise on the clock, a ripple counter being read while it changesDebounce; use a synchronous counter; latch the value before reading
Shift register shows garbageWrong clock edge or bit order, a missing latch pulse, a chain too long for the clockCheck the datasheet timing and the order of bits; slow the clock
Works slowly but fails fastSetup or hold violation, a long path, supply droopSlow the clock; shorten the path; check the timing numbers
Glitches on an outputSeveral inputs changing at once into combinational logicRegister the output with a flip-flop; add a small filter if it is slow

Related

FAQ

What is the difference between a latch and a flip-flop?

A latch is transparent while its enable is active and holds when it is not. A flip-flop samples its input only at the clock edge, so its output changes at one defined instant.

Why does a floating CMOS input cause trouble?

It has an enormous resistance, so it picks up noise and drifts through the region where both transistors conduct, drawing current and giving random outputs. Tie unused inputs high or low.

Can I connect a 5 V HC output to a 3.3 V input?

Only if the 3.3 V input is 5 V tolerant. Otherwise use a divider (for slow signals), a level shifter or an LVC buffer powered from 3.3 V.

How do I debounce a switch with logic?

Use an SR latch (two NAND gates) with a changeover switch, a Schmitt-trigger inverter after an RC filter, or a debounce chip. In software, ignore changes for 10–20 ms.

Should I use logic chips or a microcontroller?

A microcontroller for anything with decisions and sequences. Logic chips for fast, simple or parallel jobs, to add pins, to shift levels or to glue chips together, and where a program is not wanted.

References

  • Texas Instruments and Nexperia, 74HC/HCT/LVC logic family datasheets and the Logic Guide; onsemi and Texas Instruments 4000-series datasheets.
  • Horowitz, P. and Hill, W., The Art of Electronics, 3rd ed., Cambridge University Press: chapters on digital electronics and logic families.
  • Wakerly, J. F., Digital Design: Principles and Practices, Pearson.
  • IEEE 91 / IEC 60617, Graphical symbols for logic functions.

Last reviewed: October 2026. Values are typical: check the datasheets of the parts you use.