id: 5
title: Bitwise Magic
subtitle: The secret life of 0s and 1s
description: Learn the AND, ORR, EOR, and MVN instructions

tutorial:
  - title: The binary world
    text: >
      Computers store everything as **0**s and **1**s. 42 in binary is `00101010`.
      The state panel on the right shows the binary form of every register!
  - title: AND — both must be 1
    text: >
      AND compares bit by bit, returning 1 only when both bits are 1. Great for
      "extracting" specific bits:
    code: |
      ;   11111111  (255)
      ; AND 00001111  (15)
      ; = 00001111  (15)
      AND R0, R0, #15
  - title: Other bitwise ops
    text: >
      **ORR** — 1 if either bit is 1 (OR)

      **EOR** — 1 if the bits differ (XOR)

      **MVN** — flip every bit (NOT)
    code: |
      ORR R0, R0, #240  ; set the upper 4 bits
      EOR R0, R0, #255  ; flip the lower 8 bits
      MVN R0, R0        ; flip every bit

goal: R0 = **255** (binary 11111111). Use AND to extract the **lower 4 bits** so R0 becomes **15**

initialState:
  registers:
    R0: 255

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

hints:
  - "AND keeps the bits you want and clears the rest"
  - "The mask for the lower 4 bits is 15 (binary 00001111)"
  - "Answer: AND R0, R0, #15 / XHLT"

starThresholds: [2, 3, 5]

starterCode: |
  ; R0 = 255 (binary 11111111)
  ; Use AND to extract the lower 4 bits


  XHLT

showMemory: false
