easyBit ManipulationPure DSA~10 min
Permission Bitmap Toggle
An access-control service represents a user's permissions as a 32-bit integer (each bit is one capability). The admin UI lets a reviewer toggle one permission at a time. Implement the toggle and read operations on the bitmap.
Problem
Implement two functions: toggle(bitmap, i) flips the i-th bit (0-indexed) of a non-negative 32-bit integer and returns the new bitmap. has(bitmap, i) returns whether the i-th bit is currently set.
Input
Integer bitmap (0 ≤ bitmap ≤ 2^31 - 1) and integer i (0 ≤ i ≤ 30).
Output
toggle returns the updated bitmap; has returns a boolean.
Constraints
- 0 ≤ bitmap ≤ 2^31 - 1 (signed 32-bit positive range)
- 0 ≤ i ≤ 30 (we stay within signed 32-bit, no sign-bit weirdness)
- O(1) time per operation
Examples
Example 1
Input
toggle(0b1010, 0)
Output
0b1011
Bit 0 was 0; flipped to 1. Result: 11.
Example 2
Input
toggle(0b1011, 0)
Output
0b1010
Bit 0 was 1; flipped to 0. Result: 10.
Example 3
Input
has(0b1010, 1)
Output
true
Bit 1 is set.