SYSTEMS / Learn
Saved on this device>
← Back to goals

I want to…

Convert bits to hexadecimal

Trace this example, understand why the operation works, then try it from memory.

1 · Choose a goal → 2 · Learn the method → 3 · Practise → 4 · Spaced review

Learn the method

x → base 168-bit word · arithmetic wraps modulo 256
BIT-BY-BIT WALKTHROUGH
OperatorBits 7 → 0Decimal
x1001 10012153
Result1001 10012153

1001₂ = 9₁₆ · 1001₂ = 9₁₆

Each nibble becomes one hexadecimal digit: 0x99, the same value as 153.

IN CODEC17
#include <stdint.h>
#include <stdio.h>
uint8_t x = 153;

printf("%02X\n", (unsigned)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.1, §2.2.2

coffee-bytes / Systems LearningCS:APP + Hacker’s Delight