MASTER
Bit Manipulation Master
Implement and verify the complete suite of bit manipulation operations in a single execution flow.
Challenge Sequence
- Initialize
reg = 0x00 - Set bit 3 (State: 00001000)
- Clear bit 3 (State: 00000000)
- Toggle bit 3 (State: 00001000)
- Read bit 3 (Result: 1)
#include <stdint.h>
// FORCE bit n to 1 (Bitwise OR)
uint32_t setBit(uint32_t r, uint8_t n) {
return r | (1U << n);
}
// FORCE bit n to 0 (Bitwise AND with
Inverse Mask)
uint32_t clearBit(uint32_t r, uint8_t n) {
return r & ~(1U << n);
}
// FLIP bit n (Bitwise XOR)
uint32_t toggleBit(uint32_t r, uint8_t n) {
return r ^ (1U << n);
}
// EXTRACT value of bit n (Shift and
Mask)
uint8_t readBit(uint32_t r, uint8_t n) {
return (r >> n) & 1U;
}
int main() {
uint32_t reg = 0x00;
uint8_t n = 3;
reg = setBit(reg, n);
reg = clearBit(reg, n);
reg = toggleBit(reg, n);
uint8_t bit = readBit(reg, n);
return 0;
}