Bit Shifting
Shift bits left and right inside a register. Shifting left multiplies by 2, shifting right divides by 2 - the basis of fast arithmetic.
Bit shifting slides the entire binary pattern inside a register left or right by a given number of positions. Shifting left by 1 is equivalent to multiplying by 2, and shifting right by 1 is equivalent to integer division by 2. This works because binary is base-2: moving a digit one place left multiplies its value by 2, just as moving a decimal digit left multiplies by 10. CPUs can shift in a single cycle, making it far faster than multiplication.
Simulator
Walkthrough Guide
- 1
Load the starting value
Do this: Step through MOV R0, #3
Watch for: R0 = 3 (binary 00000011)
We start with 3, which has bits 0 and 1 set. Watch how the entire bit pattern slides as we shift.
- 2
Shift left twice (multiply by 4)
Do this: Step through both SHL R0, #1 instructions
Watch for: R0 goes from 3 to 6 to 12. The bit pattern slides: 00000011 → 00000110 → 00001100
Each left shift moves every bit one position toward the most significant bit and fills bit 0 with 0. The numeric effect is multiplication by 2: 3×2=6, 6×2=12.
- 3
Shift right twice (divide by 4)
Do this: Step through MOV R1, R0 then both SHR R1, #1 instructions
Watch for: R1 goes from 12 to 6 to 3. The bit pattern slides back: 00001100 → 00000110 → 00000011
Each right shift moves every bit one position toward the least significant bit and fills the top bit with 0. The numeric effect is integer division by 2: 12÷2=6, 6÷2=3.
- 4
Compute a power of 2
Do this: Step through MOV R2, #1 then SHL R2, #3
Watch for: R2 goes from 1 to 8. Binary: 00000001 → 00001000
Shifting 1 left by N positions computes 2^N. Here 1 << 3 = 8 = 2³. This is a common trick for setting a specific bit or computing powers of two without multiplication.
Objectives
- •Watch R0 double from 3 to 6 to 12 as bits shift left
- •Watch R1 halve from 12 to 6 to 3 as bits shift right
- •See R2 become 8 - shifting 1 left by 3 computes 2^3
- •Notice how shifting is just moving the entire bit pattern sideways
Hints
- Look at the binary view of each register to see the bit pattern slide left or right