Intuition The one core idea
A CPU can only read a row of on/off switches, so every instruction must become a fixed pattern of bits split into labelled boxes called fields . Master how many bits each box needs and why the "what-to-do" box never moves, and every instruction format ever built becomes readable.
This page assumes you have seen nothing . We will build every symbol the parent note Instruction formats and encoding uses — one at a time, each one anchored to a picture, each one earned before we use it. Nothing here is memorised; everything is derived.
A bit is a single switch that is either 0 (off) or 1 (on) . That is the only thing the hardware physically stores. Everything else — numbers, letters, instructions — is just many bits standing in a row.
Figure s01 below draws a row of eight bulbs: lit bulbs (butter-yellow) are 1s, dim grey bulbs are 0s. Look at the leftmost bulb — it is one bit, and the little arrow points to it. The green line underneath brackets all eight bulbs together; that grouping is what we will call a byte in a moment.
Intuition Why bits and nothing fancier
Hardware is made of transistors, and a transistor most reliably tells apart just two voltage levels — high and low. Two levels = two symbols = one bit. We could try to store ten voltage levels per wire, but telling them apart reliably is hard and noisy, so we build everything from the humble two-state switch.
A byte is a fixed bundle of 8 bits . A word is the natural chunk of bits a given CPU handles at once — on the machines in this chapter (RISC-V, MIPS, ARM) a word is 32 bits = 4 bytes.
Why 8? Historically 8 bits is enough to name every character (2 8 = 256 symbols) and is a convenient power of two, so it became the universal unit of memory addressing.
Why 32 here? The whole point of a fixed-width ISA is that every instruction is exactly one word; 32 bits is wide enough to hold an opcode plus several register numbers plus a useful immediate (we prove the exact fit in Section 6), yet small enough to fetch quickly.
If one bit holds 2 possibilities, what does a group of b bits hold?
Add one bit → each old pattern can now end in 0 or 1 → the number of patterns doubles . So:
1 bit → 2 patterns
2 bits → 4 patterns
3 bits → 8 patterns
b bits → 2 b patterns.
Figure s02 turns this into a bar chart: each bar is taller than the one before it by a factor of two, and the coral arrows between bars say "×2 again". That visual doubling is the meaning of 2 b .
Worked example Counting patterns
With 3 bits: 2 3 = 8 patterns, namely 000 , 001 , 010 , 011 , 100 , 101 , 110 , 111 . That is exactly the numbers 0 through 7 — eight things.
We keep saying "10110 is a number" — let us earn that, because the whole topic leans on it.
Definition Positional weight
In binary, each bit position carries a weight that is a power of two. Counting from the rightmost bit as position 0 , position i has weight 2 i . The number is the sum of the weights wherever a bit is 1 .
Take 10110 (five bits). Line up positions from the right:
2 4 = 16 1 2 3 = 8 0 2 2 = 4 1 2 1 = 2 1 2 0 = 1 0
Add the weights where the bit is 1 : 16 + 4 + 2 = 22 .
Intuition Why powers of two, and why count from the right
Each step left is a fresh "double or not" choice, so its worth is twice the position to its right — exactly the doubling from Section 1, now read as place value . We start counting at the right because that is where the smallest weight (2 0 = 1 ) lives, just like the ones digit sits on the right in ordinary decimal.
The parent note writes b = ⌈ log 2 N ⌉ . Let us earn every symbol.
The question we are really asking: "I have N different things (say 32 registers). How many bits b do I need so that 2 b is at least N ?"
log 2 (logarithm base 2)
log 2 N is the answer to the question "2 to the what power gives N ? " It undoes the "2 b " from Section 1.
log 2 8 = 3 because 2 3 = 8 .
log 2 32 = 5 because 2 5 = 32 .
Intuition Why a logarithm and not, say, division
We need the inverse of "raising 2 to a power", because bits combine by doubling , not by adding. Division would answer a repeated-subtraction question; here each new bit multiplies the count by 2, so the matching inverse is a logarithm base 2. It is the only tool that turns "how many doublings?" into a number.
But log 2 100 = 6.64 … — you cannot have 6.64 switches. Bits are whole, and 6 bits (= 64 patterns) is too few. We must round up to 7.
Figure s03 puts 2 5 = 32 , 2 6 = 64 , 2 7 = 128 on a line and drops a coral marker at "need 100". You can see 64 sits left of the marker (too few) and 128 sits right (fits), so the answer must round up from 6.64 to 7 .
⌈ x ⌉
⌈ x ⌉ (read "ceiling of x ") means "round x up to the next whole number". ⌈ 6.64 ⌉ = 7 , and ⌈ 5 ⌉ = 5 (already whole).
Worked example The degenerate case
N = 1
If there is only one thing to name, log 2 1 = 0 (because 2 0 = 1 ), so b = 0 : a zero-width field . That is correct — with one choice there is nothing to encode, so no bits are needed. In real ISAs a field is never literally 0 bits (there is nothing to lay out), but the formula honestly reports "this needs no information". For every N ≥ 2 you get b ≥ 1 , the usual case.
log 2 100 = 6.64 , so use 6 bits."
Why it feels right: 6.64 is closer to 7 but 6 is a whole number you already have.
The flaw: 6 bits give only 2 6 = 64 patterns — you cannot name all 100 things. You must round up , never to nearest.
Fix: always ceiling. 2 ⌈ l o g 2 N ⌉ ≥ N is guaranteed; rounding down breaks it.
Before we name fields we must agree how to count the bit positions — otherwise "bits 0 through 6" is ambiguous.
Definition Bit-indexing convention
We number bits by their positional weight from Section 1 : the rightmost bit is bit 0 (weight 2 0 ), the next left is bit 1 (weight 2 1 ), and so on up to bit 31 (the leftmost, weight 2 31 ) in a 32-bit word. So "bit 0" always means the low, rightmost end, and higher-numbered bits sit further left . This matches how the CPU reads a binary value, so a field's bit range and its numeric weight agree.
A field is a fixed slice of the instruction word that has been given a name and a meaning . "Bits 0 through 6 are the opcode field" means: those seven rightmost switches, taken together as one binary number, tell the CPU which operation to run.
Figure s04 draws the 32-bit word as six rounded, pastel boxes. Notice the labels along the bottom: "high bits (left)" and "bit 0 (right)" — that is our convention made visible. The coral opcode box sits at the far right (bits 0–6); the arrow reminds you the decoder looks there first .
The two field-names the parent leans on:
opcode — the "what job " field (add? jump? load?). Always in the same position so the decoder finds it first.
operand fields — the "what to do it to ": register numbers, or an immediate.
Intuition Why the opcode position is frozen
The Instruction Decode Stage must slice the word before it knows what the instruction is. If the opcode could hide in a different spot per instruction, decode would face a chicken-and-egg problem: "to find the opcode I must first read the opcode." Freezing its position breaks the loop — the CPU always looks at the same bits first.
A register is one of a small set of ultra-fast storage boxes inside the CPU (RISC-V has 32 of them). An instruction refers to a register by its number , so rs1, rs2, rd in the parent are just 5-bit fields holding a register number 0 –31 . See Registers and the Register File for the boxes themselves.
32 registers ⇒ b = ⌈ log 2 32 ⌉ = 5 bits per register field. That "5" you keep seeing is not magic — it is Section 2 applied to 32.
An immediate is a constant number written directly inside the instruction itself (the 100 in addi x1, x2, 100). It rides along in the bits instead of living in a register — hence "immediate", available immediately.
Intuition Why immediates need care later
An immediate stored in, say, 12 bits must be widened to the ALU's full 32 bits before use. Doing that without changing the value — especially for negative numbers — is the job of sign extension.
addi x1, x2, -8 must work, so a field must be able to mean a negative number.
Building it from the positional weights of Section 1. In an unsigned b -bit number every bit has weight + 2 i . Two's complement changes exactly one thing: the top bit's weight becomes negative , − 2 b − 1 , while every lower bit keeps its usual + 2 i .
Definition Two's complement
A b -bit two's-complement number gives the top (leftmost, bit b − 1 ) position the weight − 2 b − 1 and every lower position i the weight + 2 i . The value is still "sum the weights where the bit is 1", just with that one negative weight.
Watch it work on 4 bits (b = 4 , so the top weight is − 2 3 = − 8 ):
1000 = − 8 , 1111 = − 8 + 4 + 2 + 1 = − 1 , 0111 = 4 + 2 + 1 = + 7.
Intuition Why the top bit gets weight
− 2 b − 1
With all other weights positive, the only way to reach a negative total is to let the highest, heaviest position subtract . Turning its + 2 b − 1 into − 2 b − 1 is the smallest possible change that unlocks negatives — and it makes ordinary binary addition "just work" across the zero boundary, which is why hardware loves it. Because the top bit alone decides whether the big negative weight is present, it doubles as the sign flag : 1 means negative, 0 means non-negative.
The range then falls straight out of the weights. The most negative value turns on only the top bit: 1000 … 0 = − 2 b − 1 . The most positive turns on every bit except the top: 0111 … 1 = 2 b − 1 − 1 . So:
range = − 2 b − 1 to 2 b − 1 − 1.
For the 12-bit immediate the parent quotes: − 2 11 to 2 11 − 1 = − 2048 to + 2047 . See Two's Complement and Sign Extension for the full treatment.
Definition Sign extension
Sign extension widens a signed number to more bits by copying its top (sign) bit into all the new high bits. That keeps the value the same: − 8 in 12 bits becomes − 8 in 32 bits, not a huge positive number.
copy the sign bit and not pad with zeros
Padding with zeros would make every negative number suddenly look positive (its sign bit would no longer be on top). Copying the sign bit preserves "negativeness" all the way up. Picture stretching a number leftward while dragging its sign flag along with it.
Now every symbol in the parent's tally makes sense on sight:
7 f u n c t 7 5 r s 2 5 r s 1 3 f u n c t 3 5 r d 7 o p co d e = 32 bits
7 , 5 , 5 , 3 , 5 , 7 are field widths in bits (Section 3).
Each 5 came from ⌈ log 2 32 ⌉ (Sections 2 & 4).
The sum being exactly 32 is the fixed budget the parent keeps stressing.
Definition funct3 and funct7
add and sub (and many others) share the same 7-bit opcode class 0110011 . funct3 (a 3-bit field) and funct7 (a 7-bit field) are extra selector fields that fan that one opcode out into many distinct operations. Think of the opcode as the family surname and funct3/funct7 as the first names that pick the exact instruction inside the family.
Why split into two selectors? Keeping the opcode small preserves the fixed budget, and having a coarse selector (funct3) plus a fine one (funct7) lets the decoder branch cheaply: it can decide most of the operation from funct3 alone and only consult funct7 when it must (e.g. to tell add from sub).
Related building topics you can now approach: Addressing Modes (how operand fields name memory), RISC vs CISC (fixed vs variable width philosophy), Pipelining (why fast fixed decode matters).
b bits give 2 to the b patterns
positional weights map bits to a number
log base 2 undoes the doubling
ceiling rounds bits up to whole
field = named slice of bits
register number field 5 bits
immediate constant in the bits
two complement flips top weight negative
sign extension widens keeping value
Instruction format and encoding
Say each answer out loud before revealing it.
A bit stores how many possible values Exactly two — 0 or 1.
How many bits in a byte, and in a word here A byte is 8 bits; a word is 32 bits (4 bytes) on these machines.
How many distinct patterns fit in b bits 2 b .
What number is binary 10110 , and how 16 + 4 + 2 = 22 , summing the position weights 2 i where a bit is 1.
Which end is bit 0 The rightmost (lowest-weight, 2 0 ) bit; higher indices go leftward.
log 2 N answers what question"2 to the what power gives N ?" — it undoes 2 b .
Why the ceiling ⌈ ⌉ in b = ⌈ log 2 N ⌉ Bits are whole numbers, so we round up so 2 b ≥ N .
What does the formula give for N = 1 b = 0 — a zero-width field, since one choice carries no information.
How many bits to index 32 registers ⌈ log 2 32 ⌉ = 5 .
What is a field A fixed, named slice of the instruction word with an agreed meaning.
Which field's position is frozen, and why The opcode, so decode can always find "what job" first without a chicken-and-egg problem.
What do funct3 and funct7 do Sub-select the exact operation inside one shared opcode family (e.g. tell add from sub).
Difference between a register operand and an immediate A register field holds a number naming a storage box; an immediate holds the constant value itself .
In two's complement, what weight does the top bit carry − 2 b − 1 (negative), while all lower bits keep + 2 i .
Range of a 12-bit two's-complement immediate − 2048 to + 2047 , i.e. − 2 11 to 2 11 − 1 .
Why sign extension copies the top bit instead of padding zeros To keep negative numbers negative; zero-padding would turn them into large positives.