Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Hexadecimal
- Base 16. Each hex digit stands for exactly four bits, so a 16-bit word is four hex digits: 0x8C85 = 1000 1100 1000 0101.
- Bit field
- A run of adjacent bits inside a word that stores one value, at documented positions and with a documented unit.
- Mask
- A value with ones where bits should be kept. AND keeps them; AND with the complement clears them.
- Shift
- Moving bits left or right by a number of positions. Shifting right by 4 drops the lowest four bits.
- Flag
- A single bit that records a yes/no condition, such as whether a reading is valid.
- Read-modify-write
- Changing one field by reading the whole word, clearing that field, setting the new bits and writing the word back.
Why this matters
Hardware registers, network headers, file formats and permission sets pack several values into one integer. Reading a field without its mask leaks neighboring bits into the value; acting on a reading before its flags can page someone about stale data; and a careless test-fixture edit can flip a fault bit and test the wrong paging branch.
This lesson assumes binary place value and the bitwise operators from Fundamentals. The layout comes from the device documentation; byte order on the wire is outside it.
A number can contain several fields
Binary writes an integer in powers of two. Hexadecimal groups that representation into four bits per digit, making a packed word easier to inspect. A bit field reserves specified positions for one value; its offset says where it starts and its width says how many bits belong to it.
A shift aligns positions, while a mask selects the bits of interest. The selected bits still need their documented meaning: a flag may indicate validity, and a numeric count may need a scale to become rpm. Writing a field also has to preserve the neighboring fields. The documented layout determines how these operations interpret the word.
01Read the layout
Read the documented layout
A data centre’s monitoring agent reads each server’s fan controller over SMBus. The controller’s datasheet packs two flags, a 10-bit speed and a counter into one 16-bit status register. The fleet dashboard shifted the speed into place but kept the flag bits, so the reading exceeded the field’s documented range. Every field needs its position, its width and its unit.
| Bits | Field | Meaning |
|---|---|---|
| 15 | VALID | 1 when the speed is a current reading |
| 14 | FAULT | 1 when the controller reports a fault |
| 13–4 | SPEED | 10-bit count, 10 rpm per count (0–10,230 rpm) |
| 3–0 | SEQ | Rolling sequence counter |
The word 0xA4B3 is 1010 0100 1011 0011 in binary. Bit 15 is the leftmost. The speed field starts four bits up and is ten bits wide.
02Shift, then mask
Shift the field down, then mask its width
const SPEED_SHIFT = 4; // speed occupies bits 13..4
const SPEED_MASK = 0x3ff; // ten bits, 10 rpm per count
const VALID = 0x8000; // bit 15
const word = 0xa4b3;
const speed = ((word >> SPEED_SHIFT) & SPEED_MASK) * 10;
const updated = (word & ~(SPEED_MASK << SPEED_SHIFT)) | ((2500 / 10) << SPEED_SHIFT);
console.log(`speed ${speed} rpm`);
console.log(`valid ${(word & VALID) !== 0 ? "yes" : "no"}`);
console.log(`updated ${updated.toString(16).toUpperCase()}`);
With these number operands, bitwise operators use 32-bit integers and ~ returns a signed 32-bit result; ANDing it with a 16-bit word still leaves a 16-bit value. == and === bind tighter than &, so write (word & VALID) === 0; without parentheses the compiler rejects it (TS2363) and plain JavaScript computes word & false.
Each version prints speed 5870 rpm, valid yes and the word with
a new speed written in, updated 8FA3, which the next sections explain.
03Read the flags first
Read the flags before the reading
The speed bits always contain something, even when the controller has stopped updating them. VALID says whether they are current; FAULT says the controller itself has a problem. A paging rule for slow fans must decide the flags first: a fault pages whatever the speed says, and an invalid reading pages nobody about speed.
04Change one field
Change one field, keep the rest
The monitor’s tests need status words on either side of a speed threshold, including words with a fault or stale flag. Build a new fixture from an existing word, changing only its speed. This edits test data, not the physical controller. For 2,500 rpm (250 counts = 0xFA), clear bits 13–4, then OR in the new count shifted into place. The mask for the field is 0x3FF shifted left by 4, 0x3FF0, and its complement clears exactly those bits.
A value must also fit its field. 10,240 rpm is 1,024 counts, 0x400; shifted left by 4 it becomes 0x4000, which is the FAULT bit. Reject speeds above 10,230 rpm or between multiples of 10 instead of letting them spill into a neighbor.
05Vary inputs, then decide
Vary one input at a time, then decide
| Word | VALID / FAULT | Speed | Decision |
|---|---|---|---|
| 0xA4B3 | 1 / 0 | 5,870 rpm | ok |
| 0x8FA3 | 1 / 0 | 2,500 rpm | ok |
| 0x8BB0 | 1 / 0 | 1,870 rpm | page |
| 0x0BB0 | 0 / 0 | 1,870 rpm | stale |
| 0x4FA0 | 0 / 1 | 2,500 rpm | page (fault) |
06Decode the fan word
Build one status-word decoder
Decode a captured word so the monitor reports rpm rather than raw counts. Then make its paging decision respect fault and validity flags. Finally, build fixture words that vary the speed while preserving those flags and the sequence counter, so tests exercise the branch they intended. Each task supplies the other working helpers.
A fan controller
Decode the speed, trust the flags, then build focused test fixtures.
In the frontend Hex colors in Next.js and Svelte Optional
A CSS color written #rrggbb is a 24-bit word with three 8-bit fields. Parse
the six hex digits once, then shift each channel down to bit 0 and mask it with 0xff. Reject other formats explicitly rather than guessing.
The Next.js component parses during render; the Svelte component derives the channels
from its color prop with $derived.by. Both accept only #rrggbb.
type Rgb = { r: number; g: number; b: number };
export function parseHex(color: string): Rgb | null {
if (!/^#[0-9a-fA-F]{6}$/.test(color)) return null;
const value = parseInt(color.slice(1), 16); // 24 bits: rrggbb
return { r: (value >> 16) & 0xff, g: (value >> 8) & 0xff, b: value & 0xff };
}
export function ColorLabel({ color }: { color: string }) {
const rgb = parseHex(color);
if (!rgb) return <span>unsupported color</span>;
return <span>{`rgb(${rgb.r} ${rgb.g} ${rgb.b})`}</span>;
}
Check this idea later
Sources
- Go: operator precedence and &^
- Rust: expression precedence
- C++: bitwise AND and integral promotions
- ECMAScript: binary bitwise operators
- CSS Color 4: hex colors
The status-word layout is a worked model, not a specific vendor’s register. Precedence behavior was checked on Go 1.27, rustc 1.99, clang 17 and the TypeScript compiler.
Sign in to keep your read marks on every device. Sign in
Was this helpful?