Cover of Code by Charles Petzold, featuring an abstract representation of binary signals and digital logic circuits on a dark background

Pages

562

Published

2022

Programming ✨ New

Code

The Hidden Language of Computer Hardware and Software

Understand exactly how a computer works, from a telegraph relay to a modern CPU, by building the logic up from first principles.

Code by Charles Petzold traces the path from simple electrical signals to the fully functioning digital computer, explaining every layer in between without assuming prior knowledge. Starting with Morse code and relay switches, Petzold constructs logic gates, memory, arithmetic units, and eventually a working CPU from the ground up. The result is a rare book that gives programmers a genuine mental model of what the machine is actually doing when their code runs.

About this book

Most programmers write code without ever knowing what happens below the operating system, the compiler, or even the instruction set. Code closes that gap. Charles Petzold starts with the simplest possible building block — a flashlight circuit — and, step by step, constructs everything a computer needs to function: logic gates, binary arithmetic, memory cells, a CPU, and an assembler.

The approach is cumulative and unhurried. Each chapter introduces exactly one new idea, explains why it exists, and connects it to everything that came before. By the time you finish, you will not merely believe that computers work in a certain way — you will understand the chain of reasoning that makes it inevitable.

The second edition, published in 2022, brings the narrative up to date with modern processors, Unicode, floating-point standards, and contemporary operating system concepts, while preserving the patient, first-principles style that made the first edition a lasting reference on programmers' shelves.

This book is not a tutorial for a specific language or framework. It is an explanation of the substrate beneath all of them. Readers who finish it report that debugging, performance analysis, and low-level systems work feel qualitatively different afterward — because they are working with a complete picture rather than a set of memorized rules.

  • Trace the path from binary digits to Boolean logic to working arithmetic circuits
  • Understand how RAM, ROM, and storage are built from simpler switching elements
  • See how an instruction set and a CPU are designed from scratch
  • Follow how an assembler turns human-readable mnemonics into machine code
  • Learn how operating systems manage memory, processes, and I/O at the hardware level

Whether you are a self-taught developer filling in gaps, a computer science student wanting the intuition behind your coursework, or an experienced engineer who has simply never had time to look this far down the stack, Code gives you a durable mental model that no amount of framework-specific documentation can replace.

🎯 What you'll learn

  • Trace the logical path from a simple electrical relay to a fully functioning CPU
  • Construct binary addition, subtraction, and multiplication circuits from Boolean gates
  • Explain how RAM and ROM store and retrieve information at the transistor level
  • Read and write basic machine code and assembly language for a simplified processor
  • Describe how a compiler or assembler translates source code into executable instructions
  • Understand how floating-point numbers are represented and why rounding errors occur
  • Explain what an operating system does in concrete hardware terms, not just abstract metaphors
  • Apply a first-principles mental model when debugging, profiling, or reasoning about performance

👤 Who is this book for?

  • Self-taught developers who write working code but want to understand what the hardware is actually doing
  • Computer science students who need the intuition to match the formal theory in their coursework
  • Experienced engineers who have never had reason to study hardware but want to close that gap
  • Technical writers, educators, or architects who explain software systems and want to explain them accurately all the way down
  • Curious non-specialists with a strong interest in how technology works at a fundamental level

Table of contents

  1. 01

    Best Friends

    Introduces Morse code as the book's first working communication system and uses it to establish the core idea that information can be encoded in simple on/off signals. You learn why binary representations are both natural and powerful.

  2. 02

    Codes and Combinations

    Explores how different coding systems — Braille, Morse, and others — map symbols to patterns. You see how the number of bits available determines how many distinct symbols you can represent.

  3. 03

    Electricity and the Telegraph

    Introduces basic electrical circuits, voltage, current, and the telegraph relay. You learn how a relay amplifies a weak signal and why that property is the foundation of all digital logic.

  4. 04

    Anatomy of a Flashlight

    Builds a precise model of a simple circuit and introduces the vocabulary of switches, conductors, and current flow that the rest of the book depends on.

  5. 05

    Logic Gates

    Shows how combining electrical relays produces AND, OR, and NOT gates. You build each gate from first principles and verify its behavior with truth tables.

  6. 06

    Binary Arithmetic

    Derives binary addition from Boolean logic and constructs a half-adder and then a full-adder from gates you already understand. You see how a CPU's arithmetic unit emerges from this same pattern.

  7. 07

    Bytes and Memory

    Explains how individual bits are grouped into bytes and stored in latches and flip-flops. You construct a simple memory cell and understand how RAM holds a value between clock cycles.

  8. 08

    Automation

    Introduces the concept of clocked, sequential logic and shows how a counter and a simple state machine can automate a repetitive computation. You see why synchronization matters in real processors.

  9. 09

    The Central Processing Unit

    Assembles all the pieces — the ALU, registers, program counter, and control unit — into a working CPU design. You trace the fetch-decode-execute cycle for a small instruction set step by step.

  10. 10

    From Machine Code to Operating Systems

    Follows the journey from raw machine instructions through assemblers, high-level languages, and finally to what an operating system provides: memory management, processes, file systems, and a clean interface for programs to run on top of.

Frequently asked questions

Do I need a background in electronics or computer science to read this book?

No. Petzold assumes only curiosity and basic arithmetic. Every concept is introduced from scratch, including the electrical fundamentals.

Is this book for programmers who already write code, or is it aimed at complete beginners?

It is genuinely useful at both ends. Working programmers use it to fill conceptual gaps; beginners use it to build a solid foundation before learning a language.

How does the second edition (2022) differ from the first edition?

The second edition adds coverage of modern processors, Unicode, IEEE floating-point, and contemporary OS concepts. The patient first-principles style of the original is unchanged.

Is this a hands-on coding book with exercises and projects?

No. Code is a conceptual and explanatory book. You follow logical constructions and thought experiments rather than typing code into a compiler. Think of it as a carefully argued explanation rather than a tutorial.

Is this book still relevant given how much hardware has changed since the first edition?

Yes. The principles Petzold explains — Boolean logic, binary arithmetic, the fetch-decode-execute cycle — are the same in every processor manufactured today. The second edition also addresses modern specifics directly.

You might also like

📬 Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.