Learn the method
~(x & y) = ~x | ~y8-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
y | 1000 11112 | 143 |
x & y | 1000 10012 | 137 |
Result | 0111 01102 | 118 |
The result is 01110110. De Morgan gives the same result as (NOT a) OR (NOT b), at the same width.
#include <stdint.h>
uint8_t x = 153;
uint8_t y = 143;
uint8_t result = (uint8_t)~(x & y);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