Skip to content
Tactiverge

△ Embedded systems · bitwise operators

Set, clear and test register bits

Turn individual bits of an 8-bit control register on and off without disturbing the others.

  • Level 2 · Builder
  • 7 tests
  • 🐞 debug variant
  • technically checked

Your task

Each bit of a microcontroller control register switches something: bit 3 might enable a timer, bit 7 an interrupt. You must change ONE bit and leave the rest alone.

Write, for 8-bit values (0–255):

  • setBit(reg, n) → reg with bit n forced to 1: reg | (1 << n)
  • clearBit(reg, n) → reg with bit n forced to 0: reg & ~(1 << n) (then keep it 8-bit with & 0xff)
  • isSet(reg, n) → true if bit n is 1.

Hints (0 of 3 shown)

    What gets checked

    • setBit(0, 3) → 8
    • clearBit(255, 0) → 254
    • isSet(5, 2) → true

    Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.