Learn the method
x & ~(1 << 1)8-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
1 << 1 | 0000 00102 | 2 |
~(1 << 1) | 1111 11012 | 253 |
Result | 1001 10012 | 153 |
a AND NOT (1 << 1) gives 10011001. Only the selected bit is cleared.
#include <stdint.h>
uint8_t x = 153;
uint8_t result = x & ~(1u << 1);C17 · snippets use uint8_t from <stdint.h>. Casts keep the low 8 bits; shifts use unsigned operands. Run the statements inside a function.
CS:APP · §2.1.8