id: 6
title: Shift Operations
subtitle: A dance of bits
description: Learn the LSL and LSR instructions

tutorial:
  - title: LSL — Logical Shift Left
    text: >
      All bits move left, zeros fill in on the right. **Shifting left by 1 = multiplying by 2**.
      Shifting left by 3 = multiplying by 8:
    code: |
      ; 5 = 00000101
      LSL R0, R0, #1  ; 00001010 = 10 (×2)
      LSL R0, R0, #1  ; 00010100 = 20 (×2)
  - title: LSR — Logical Shift Right
    text: >
      All bits move right, zeros fill in on the left. **Shifting right by 1 = dividing by 2**:
    code: |
      MOV R0, #40
      LSR R0, R0, #1  ; 20 (÷2)
      LSR R0, R0, #2  ; 5  (÷4)
  - title: A programmer's trick
    text: >
      On a real ARM CPU, shifts are far cheaper than multiply/divide!
      `LSL R0, R0, #3` beats `MUL R0, R0, #8`.

goal: R0 = **5**. Use **shift only** to make it **40** (40 = 5 × 8 = 5 × 2³)

initialState:
  registers:
    R0: 5

testCases:
  - init: {}
    expected:
      registers:
        R0: 40

hints:
  - "8 = 2³ — multiplying by 8 is shifting left by 3"
  - "LSL R0, R0, #3"
  - "It's just one instruction!"

starThresholds: [2, 3, 5]
blockedOps: [MUL, XDIV]

starterCode: |
  ; R0 = 5
  ; Use LSL to turn R0 into 40 (no MUL allowed)


  XHLT

showMemory: false
