Learn the method
ashr₈(x, 3)8-bit word · arithmetic wraps modulo 256| Operator | Bits 7 → 0 | Decimal |
|---|---|---|
x | 1001 10012 | 153 |
Result | 1111 00112 | 243 |
Repeating the sign bit gives 11110011. This drill explicitly specifies arithmetic shift rather than assuming a C implementation.
#include <stdint.h>
uint8_t x = 153;
uint8_t fill = (x & 128u) ? (uint8_t)(255u << (8 - 3)) : 0;
uint8_t result = (x >> 3) | fill;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.10, §2.3.7