Study Guide: Number Bases, Two’s Complement, Instructions & Bit Operations

Topics: writing numbers in different bases, two’s complement for signed values, shifts and bitwise operations, what a machine instruction is made of, and the ways an instruction can locate its operands. The practice game at the end covers everything on this page.

Key terms

Base (radix)The number of distinct digits a positional system uses. Base 10 uses 0–9; base 2 uses 0–1; base 5 uses 0–4.
Positional notationEach digit's value depends on its position: digit × baseposition, counting positions from 0 on the right.
Machine instructionA binary-encoded command the CPU can execute directly, made of an opcode and zero or more operands.
OpcodeThe part of an instruction that says what operation to perform (addu, lw, beq, …).
OperandThe part that says what to operate on — a register, a memory location, or a constant.
Addressing modeThe rule an instruction uses to figure out where an operand lives: in the instruction itself, in a register, in memory, etc.
Effective addressThe final memory address an instruction actually accesses, after its addressing mode has been applied.
Program counter (PC)The register holding the address of the next instruction to fetch.

Converting between bases

To read a number in any base, multiply each digit by that base raised to its position, then add. To go from base 10 to another base, repeatedly divide by the target base and collect remainders, reading them bottom-up.

Problem 1 · base 5 → base 10 → base 2

Convert 2345 to base 10, then convert that result to base 2.

Show worked solution
Step 1 (base 5 → base 10): 2345 = 2·5² + 3·5¹ + 4·5&sup0; = 2·25 + 3·5 + 4·1 = 50 + 15 + 4 = 6910 Step 2 (base 10 → base 2), repeated division by 2: 69 ÷ 2 = 34 remainder 1 34 ÷ 2 = 17 remainder 0 17 ÷ 2 = 8 remainder 1 8 ÷ 2 = 4 remainder 0 4 ÷ 2 = 2 remainder 0 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 Read remainders bottom-up: 10001012 Check: 64 + 4 + 1 = 69 ✓
Problem 2 · direct base 5 → base 2

Convert 2345 directly to base 2 — without going through base 10 as an intermediate answer.

Show worked solution
Divide the base-5 number by 2 in base 5 each time. The remainder is the next binary digit (least significant first): 2345 ÷ 2 = 1145 remainder 1 (bit 0 = 1) 1145 ÷ 2 = 325 remainder 0 (bit 1 = 0) 325 ÷ 2 = 135 remainder 1 (bit 2 = 1) 135 ÷ 2 = 45 remainder 0 (bit 3 = 0) 45 ÷ 2 = 25 remainder 0 (bit 4 = 0) 25 ÷ 2 = 15 remainder 0 (bit 5 = 0) 15 ÷ 2 = 05 remainder 1 (bit 6 = 1) Read bits bottom-up: 10001012 (same answer as Problem 1)

Note: base 5 isn’t a power of 2, so there’s no shortcut like grouping binary digits in threes for octal. Direct conversion here still means doing arithmetic in base 5 — the value never changes, only how it’s written.

Base-5 blocks: see the number

Each place value is a bundle of smaller pieces. In base 5, a unit is 1, a strip is 5 units, a flat is 5 strips (25 units), and a cube is 5 flats (125 units). Each place can hold at most 4 pieces before it rolls over into the next place up, just like base 10 holds at most 9.

Build it · base-5 blocks

Start with 2345 (2 flats, 3 strips, 4 units). Use the buttons to change the blocks and watch both numbers update.

Machine instructions

A machine instruction is a fixed binary pattern the CPU decodes. Its basic parts:

addu $10, $9, $8   (R-type, 32 bits)
+--------+-------+-------+-------+-------+--------+
| opcode |  rs   |  rt   |  rd   | shamt | funct  |
| 000000 | 01001 | 01000 | 01010 | 00000 | 100001 |
+--------+-------+-------+-------+-------+--------+
   6        5       5       5       5        6  bits
rs = $9 (source 1), rt = $8 (source 2), rd = $10 (destination)

Instructions with a constant, such as addi, use the I-type layout: opcode, rs, rt, then a 16-bit immediate in place of rd, shamt, and funct.

The CPU runs every instruction through the same fetch → increment → execute cycle: fetch the instruction at the address in the PC, increment the PC so it points to the next instruction, then execute the instruction (decoding its opcode and operands as part of that step). A jump changes the PC during execute instead of letting it advance by one instruction.

Field positions and register names (MIPS green card)

Formatopcode 31–26rs 25–21rt 20–16rd 15–11shamt 10–6funct 5–0
R-typeopcodersrtrdshamtfunct
I-typeopcodersrtimmediate, bits 15–0
J-typeopcodeaddress, bits 25–0
RegisterNumberUse
$zero0always 0
$at1assembler temporary
$v0–$v12–3return values
$a0–$a34–7arguments
$t0–$t78–15temporaries
$s0–$s716–23saved registers
$t8–$t924–25temporaries
$k0–$k126–27kernel
$gp28global pointer
$sp29stack pointer
$fp30frame pointer
$ra31return address

Watch the cycle

FETCH PC → memory → IR INCREMENT PC += instruction size EXECUTE decode, then ALU / memory / jump REPEAT back to FETCH repeat for every instruction
stage 0 / 4
Press Play or Step to walk through one instruction.

Common instruction categories

CategoryExample mnemonicWhat it does
Data transferlw, swCopy a word between registers and memory
Arithmeticaddu, subu, addiCompute a result from registers, or from a register and a constant
Logicaland, or, xor, nor, andi, oriBitwise operations on operands (NOT is a pseudo-instruction, written as nor with $zero)
Shiftsll, srlMove bits left or right, filling the vacated bits with 0s
Control flowbeq, bne, j, jalChange the PC, so the next instruction isn’t simply the following one

Addressing modes

The same opcode can find its operand in several different ways. The addressing mode tells the CPU which way applies. Using MIPS examples (lw loads a word from memory into a register):

ModeExampleWhere the operand comes from
Immediateaddi $t0, $zero, 5The value 5 is inside the instruction itself (a 16-bit immediate)
Direct (absolute)lw $t0, 1000($zero)The contents of memory address 1000. MIPS has no separate direct form for lw, so $zero (always 0) is the base
Registeraddu $10, $9, $8The contents of registers $9 and $8 (no memory access)
Register indirectlw $t0, 0($s0)Memory at the address held in $s0
Indexed / base+offsetlw $t0, 100($s0)Memory at address ($s0 + 100), good for array elements
PC-relativebeq $t0, $t1, 3Address = PC + 4 + 3 × 4. The green card writes this offset as OFF18, a 16-bit word offset shifted left 2
Practice · find the effective address

$s0 holds 2000. What is the effective address for lw $t0, 100($s0)? What if the mode were register indirect lw $t0, 0($s0)?

Show answer
Indexed: 2000 + 100 = 2100 (the word at memory address 2100 is loaded into $t0) Register indirect: the address is just $s0 = 2000 (the word at memory address 2000 is loaded into $t0)

Watch each mode find its operand

instruction addi $t0, $zero, 5 address / source operand is in the instruction operand value 5
step 0 / 3
Pick a mode above, then step through it.
Running MIPS: QtSpim is a MIPS simulator. You can load a MIPS program, step through it, and watch the registers and memory change. Use it to check the examples on this page, such as addu $10, $9, $8 with $9 = 34 and $8 = 5, which should leave 39 in $10.
Exam tip: the most common mix-up is immediate vs. direct. In MIPS, addi takes the number 5 directly, so the immediate is a plain number. In lw, the number before the parentheses is an offset added to the base register, so 1000($zero) means address 1000. Check your course’s notation, since some textbooks use different symbols.

Two’s complement and signed numbers

With 8 bits, the unsigned range is 0 to 255. Two’s complement uses the same 8 bits for signed numbers from -128 to 127, and the leftmost bit has weight -128. A positive number keeps its unsigned pattern. To get the negative of a number, invert every bit and add 1.

Problem 3 · two’s complement of +45 and -45

Write 45 and -45 as 8-bit two’s complement patterns.

Show worked solution
+45: 45 = 32 + 8 + 4 + 1 = 00101101 (positive, so the pattern is unchanged) -45: start with 00101101 invert every bit 11010010 add 1 11010011 Check: weights -128, 64, 32, 16, 8, 4, 2, 1 1(-128) + 1(64) + 0(32) + 1(16) + 0(8) + 0(4) + 1(2) + 1(1) = -128 + 64 + 16 + 2 + 1 = -45 ✓

Interactive version: number-representations.html, which also shows +n and -n side by side.

Shift instructions

sll d, s, shamt shifts bits left and fills the right side with 0s. srl d, s, shamt shifts bits right and fills the left side with 0s. A left shift by n multiplies by 2n. A logical right shift by n divides by 2n and drops the remainder, for values that are not negative. Both are R-type instructions, with opcode 000000. The funct field is 0x00 for sll and 0x02 for srl.

Problem 4 · two shifts in a row

$4 holds 5. Find $4 after sll $4, $4, 3, then find $4 after srl $4, $4, 2.

Show worked solution
5 = 00000101 sll 3: 00101000 = 40 (5 × 2³ = 40) srl 2: 00001010 = 10 (40 ÷ 2² = 10, remainder dropped)

Interactive version: shift-instructions.html.

Bitwise operations

Bitwise operations work on each bit column separately. No carries move between columns.

OperationMIPS formResult bit is 1 when…
ANDand $d, $s, $tboth bits are 1
ORor $d, $s, $tat least one bit is 1
XORxor $d, $s, $tthe two bits differ
NOTNOT $d, $s (pseudo); nor $d, $s, $zerothe bit is 0
Problem 5 · AND and XOR

A = 11001010, B = 10100110. Find A AND B and A XOR B.

Show worked solution
A 11001010 B 10100110 AND 10000010 = 130 XOR 01101100 = 108

Interactive version: bitwise-instructions.html, with AND, OR, XOR, and NOT one column at a time.

Interactive pages

Horner’s method in MIPS

Horner’s method evaluates a polynomial with one multiply and one add per term, by nesting the terms:

3x^4 - 2x^3 + 10x^2 - 13x + 14  =  ((((3)x - 2)x + 10)x - 13)x + 14

The MIPS program in HornersMethod.asm does this with mult and mflo. mult puts the full 64-bit product in the Hi and Lo registers, and mflo copies the low 32 bits into the destination register:

mult $10, $9        # $10 * $9 goes into Hi:Lo
mflo $10            # low 32 bits of the product go into $10

Step through the program line by line

$9 (x)-
$10-
ALU 
Hi-
Lo-
$v0-
Press Next to run the first instruction.
line 0 / 16

Each step uses the accumulator value from the step before it. The result is 440, which matches evaluating 3x4 − 2x3 + 10x2 − 13x + 14 directly at x = −3: 243 + 54 + 90 + 39 + 14 = 440.

Worked example: multiply, then move the low word (newMult.asm)

This program computes 5x − 74 with x = 12. The multiply puts the product in Hi:Lo, and mflo copies the low word into a register:

ori    $8, $0, 12      # $8 = x = 12
ori    $9, $0, 5       # $9 = 5
mult   $9, $8          # Hi:Lo = 5 × 12 = 60
mflo   $9              # $9 = Lo = 60
addiu  $9, $9, -74     # $9 = 60 + (-74) = -14

After mult, Hi is 0 and Lo is 60, because the product is positive and fits in 32 bits. The final value in $9 is −14.

Overflow and trapping

A result overflows when it doesn’t fit in the register’s bits. With two’s complement, adding two positive numbers can produce a negative result, and adding two negative numbers can produce a positive one. The sign of the result is then wrong.

Problem 6 · 8-bit overflow

Add 01111111 (127) and 00000001 (1) in 8-bit two’s complement.

Show worked solution
01111111 (127) + 00000001 (1) = 10000000 reads as -128 The correct answer, 128, doesn’t fit in 8 bits. Two positive operands gave a negative result, so the sum overflowed.

Two MIPS instructions, two behaviors

add, addi, and sub check for overflow. If the result doesn’t fit in 32 bits, the CPU raises an arithmetic overflow exception (code 12 on the green card) and does not write the result. addu and addiu never check, so they wrap around silently. The same bits come out either way; the difference is whether the program is told.

# build 0x7FFFFFFF = 2147483647 in $8
lui    $8, 0x7FFF
ori    $8, $8, 0xFFFF
addi   $9, $0, 1

add    $10, $8, $9     # overflow: raises the exception, $10 is not written
addu   $11, $8, $9     # no check: $11 = 0x80000000 = -2147483648

The result of addu is wrong for 2147483647 + 1, and nothing in the program reports it. Use add when the program must catch overflow, and addu when wrapping is intended, as in hash functions and address arithmetic.

Practice game: Bit Lab

Ten random questions cover base conversion, two’s complement, shifts, and bitwise operations. Choose an answer, then read the explanation before moving on. Refresh or press Restart for a new set.