Learn the method
(x ^ y) & 0x0F8-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
y | 1000 11112 | 143 |
x ^ y | 0001 01102 | 22 |
0x0F | 0000 11112 | 15 |
Result | 0000 01102 | 6 |
The XOR result is masked to four low bits: 00000110 (6).
#include <stdint.h>
uint8_t x = 153;
uint8_t y = 143;
uint8_t result = (x ^ y) & 0x0Fu;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