Learn the method
~x8-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
Result | 0110 01102 | 102 |
The 8-bit complement is 01100110. Its unsigned value is 255 − 153 = 102.
#include <stdint.h>
uint8_t x = 153;
uint8_t result = (uint8_t)~x;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.7, §2.1.9