Learn the method
x < 128 ? x : x − 2568-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
Result | 1001 10012 | -103 |
-128 + 16 + 8 + 1 = -103
Unsigned 153 is signed -103. The highest bit has weight −128 in this interpretation.
#include <stdint.h>
uint8_t x = 153;
int result = x < 128 ? (int)x : (int)x - 256;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.2.3, §2.2.7, §2.3.3