Numbers and bits · 11 / 15 Exercise Medium

Bit mask commands

Objective

An integer can act as a set of flags: bit i is 1 if flag i is on. With |=, &= ~ and ^= you change a flag, and with (m >> i) & 1 you query it.

Instructions

  • The mask starts at 0. The first line has the number n of commands: set i (turns the bit on), clear i (turns it off), toggle i (flips it), test i (shows 0 or 1) or show (shows the integer value of the mask).
Input
5
set 0
set 3
test 3
toggle 0
show
Output
1
8

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise