Skip to content
Tactiverge

△ Digital electronics · place value and loops

Binary ↔ decimal converter

Convert between binary strings and numbers the way a microcontroller register works.

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

Your task

Microcontroller datasheets describe registers in binary. Write:

  • toDecimal(bits): "1011" → 11. Walk the string from the right; each place is worth twice the one to its right.
  • toBinary(n, width): 5, 8 → "00000101". Repeatedly take n % 2 and halve n; pad with zeros on the left to width digits.

Try it without parseInt(bits, 2) or n.toString(2): the point is to see how place value works.

Hints (0 of 3 shown)

    What gets checked

    • toDecimal("1011") → 11
    • toDecimal("0") → 0
    • toBinary(5, 8) → "00000101"

    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.