bit-dojo: learn bitwise register tricks on a CH32V003
A free, interactive game for learning the bitwise operations embedded C is built on. Write real register code, such as GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4*1);, against a simulated CH32V003 RISC-V microcontroller, and see every bit change.
What you practise
Setting, clearing, toggling and testing bits with |=, &= ~, ^= and &
Writing and reading multi-bit register fields with masks and shifts
Real CH32V003 registers: RCC clock enables, GPIO CFGLR, OUTDR, INDR and BSHR
Classic embedded C bugs: clobbering with =, operator precedence, signed shifts and undefined behaviour
An animated step-by-step visualizer that shows how every operator works in binary
24 levels in 5 chapters
Bits & hex
1. One bit — Write an expression with only bit 5 set.
2. A nibble mask — Write a mask with bits 4, 5, 6 and 7 set — nothing else.
3. Everything but — Write a value with every bit set except bits 4-7.
4. Place a field — Put the 4-bit value 0b1001 into bits 8-11 (everything else 0).
Set · clear · toggle · test
5. Set a bit — Set bit 3 of reg. Leave every other bit alone.
6. Clear a bit — Clear bit 7 of reg. Leave every other bit alone.
7. Toggle a bit — Flip bit 0 of reg: 0 → 1, 1 → 0. Others unchanged.
8. Test a bit — Write an expression that is true (non-zero) exactly when bit 2 of reg is 1.
9. Two at once — Set bits 1 and 6 of reg, and clear bit 0. Others unchanged.
Fields
10. Clear a field — Clear bits 4-7 of reg (make them 0000). Others unchanged.
11. Write a field — Make bits 4-7 of reg hold the value 9 (0b1001). Others unchanged.
12. Read a field — Store the value of bits 8-11 of reg into val (as a number 0-15).
Real CH32V003 registers
13. Wake up port C — Turn on the clock for GPIOC. Don't disturb the other peripherals.
14. PC1 as output — Configure PC1 as a 10 MHz push-pull output. Other pins keep their setup.
15. LED on with BSHR — Drive PC1 high using GPIOC->BSHR. Other outputs unchanged.