How Instructions Are Encoded as Binary and Decoded by Your Processor
Every program you've ever run — from a simple calculator to a 3D game — is stored in memory as a sequence of binary numbers. These numbers are the machine code instructions that your CPU fetches, decodes, and executes. When I first learned that the human-readable assembly code I was studying gets turned into raw bits before the CPU sees it, the whole process felt magical. But the encoding scheme is remarkably logical once you understand the pattern.
In this article I'll break down how CPU instructions are structured in binary, walk through real examples from the x86-64 and ARM architectures, and explain the fetch-decode-execute cycle that makes it all work.
Every CPU instruction consists of two main parts:
The simplest instruction format looks like this:
[ Opcode (4-12 bits) ] [ Operand 1 (3-5 bits) ] [ Operand 2 (3-5 bits) ] [ Optional Immediate ]
For example, a RISC-V instruction that adds registers R1 and R2 and stores the result in R3 might encode as:
ADD R3, R1, R2 → 0000000 00001 00010 000 00011 0110011
|--funct7--| |rs1| |rs2| |f3| |-rd-| |-opcode-|
In this 32-bit instruction, the opcode (0110011) identifies it as an arithmetic operation, the funct3 and funct7 fields specify that it's an ADD (not SUB, XOR, or another operation), and the three 5-bit fields identify the source and destination registers.
The two dominant instruction set architectures take very different approaches to binary encoding:
RISC (Reduced Instruction Set Computer) — ARM, RISC-V:
CISC (Complex Instruction Set Computer) — x86, x86-64:
Real-world example: The same function compiled for ARM and x86-64 produces very different binary patterns. An ARM ADD X0, X1, X2 is always a clean 32-bit instruction. The x86-64 equivalent ADD RAX, RBX is just 3 bytes (48 01 D8) but might decode into 5-6 micro-ops internally.
The CPU doesn't just blindly execute instructions — it follows a precise three-step cycle for each instruction:
This cycle repeats billions of times per second. A 4 GHz CPU (common in modern laptops) completes roughly 4 billion fetch-decode-execute cycles per second, or about 0.25 nanoseconds per cycle.
Here are some real x86-64 instruction encodings in hexadecimal and binary:
MOV RAX, 42 → 48 C7 C0 2A 00 00 00
Binary: 01001000 11000111 11000000 00101010 ...
ADD RAX, RBX → 48 01 D8
Binary: 01001000 00000001 11011000
JMP 0x400100 → E9 FB 00 01 00
Binary: 11101001 11111011 00000000 00000001 00000000
CALL [RAX] → FF D0
Binary: 11111111 11010000
The prefix byte 48 appears in many x86-64 instructions — it's the REX.W prefix that tells the CPU to use 64-bit operand size instead of 32-bit. The ModR/M byte (C7 in the MOV example) encodes the addressing mode and register operands.
The instruction decoder is a large array of logic gates that translates opcode bits into control signals. Here's a simplified view of how an 8-bit opcode decoder works:
ADD detection AND gate fires when bits [6:0] equal 0110011 and funct3 bits [14:12] equal 000.A full instruction decoder in a modern OoO (Out-of-Order) CPU might contain tens of thousands of gates and consume significant chip area — especially in x86-64 where the variable-length encoding makes decoding considerably harder than in fixed-width RISC architectures.
Modern CPUs don't wait for one instruction to finish before starting the next. Instead, they use instruction pipelining — overlapping the fetch, decode, and execute stages for multiple instructions simultaneously.
A 5-stage pipeline looks like this:
Cycle 1: |F1| | | | | Cycle 2: |F2|D1| | | | Cycle 3: |F3|D2|E1| | | Cycle 4: |F4|D3|E2|M1| | Cycle 5: |F5|D4|E3|M2|W1|
During each clock cycle, the CPU is fetching instruction 5, decoding instruction 4, executing instruction 3, accessing memory for instruction 2, and writing back the result of instruction 1 — all simultaneously. This is why adding more pipeline stages (at the cost of handling hazards) has been a primary driver of CPU performance improvements over the past few decades.
Open the Binary Code Decoder in a new tab and enter some binary patterns to see the results instantly. All conversions happen in your browser — no data is sent to any server.