Bit Row
A row of equal-size bit cells (0 / 1) — the foundational primitive for any bitwise-operation visualization. Each cell shows a binary digit in a monospace font; per-bit tones model the four states most bit-manipulation algorithms touch (default, highlight, visited, dimmed); labels below each cell can show the bit index or the place value (2^k).
1
0
1
0
1
1
0
0
Customize
Value
172
8b
Display
place
Installation
npx shadcn@latest add https://craftbits.dev/r/bit-row.jsonUsage
import { BitRow } from "@craft-bits/core";
<BitRow value={0b1010_1100} bits={8} labels="place" />Highlight a single bit that "just flipped":
<BitRow
value={0b0110_1001}
bits={8}
labels="index"
tones={["default", "default", "default", "highlight", "default", "default", "default", "default"]}
/>Understanding the component
- Three value shapes.
valueaccepts anumber(up to 32 bits via unsigned coercion), abigint(arbitrary precision, lowbitsbits read), or astringof0/1digits with an optional0bprefix. Strings preserve their literal width whenbitsis omitted. - Width inference. When
bitsis omitted, the row sizes itself to the value's natural width — string length for strings,⌊log₂(n)⌋ + 1for non-zero numbers. Passbitsexplicitly when you want a fixed-width display. - Direction.
direction="msb-first"(the default) reads the row left-to-right like a binary literal.direction="lsb-first"flips the row so the LSB sits on the left — useful when the visualization tracks bit indices that "grow" rightward. - Tones are per display cell.
tones[0]is the leftmost cell, regardless of direction.highlightpaints an accent fill ("this bit matters here"),visitedpaints a success tint ("this bit just flipped"),dimmeddrops the column to 30% opacity, anddefaultlets1-cells pick up a subtle accent border so the row reads as a binary value at a glance. - Labels.
labels="index"shows0..n-1beneath each cell (lowest index always = LSB regardless of direction).labels="place"shows the place value2^k.labels="none"(the default) leaves the cells unlabelled. - Responsive width. Cell width uses
clamp(24px, …, 36px)so the row shrinks gracefully on narrow viewports and never breaks below the legibility floor. - Reduced motion. When
prefers-reduced-motion: reduceis set, the cell colour transition collapses toduration: 0— tone changes snap instead of springing.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | bigint | string | required | Binary value. number is read via unsigned 32-bit coercion; bigint is arbitrary precision; string is a 0/1 literal with optional "0b" prefix. |
bits | number | inferred | Number of cells to render. Defaults to the value's natural width. |
direction | "msb-first" | "lsb-first" | "msb-first" | Bit order on the page. |
tones | readonly BitTone[] | [] | Per-display-cell tone: default | highlight | visited | dimmed. |
labels | "index" | "place" | "none" | "none" | What appears beneath each cell. |
className | string | — | Merged onto the outer <div>. |
Accessibility
- The outer element is
role="group"with anaria-labelannouncing the binary value (Binary value: 0b10101100). - Each cell carries
role="img"and anaria-labellike"Bit 3: 0 (place value 2^3)"so a screen-reader user can read the row bit-by-bit. - Each cell exposes
data-state(the tone) anddata-bit(0/1) so consumer apps can hook custom styles or assistive tooling. - Colour is never the only signal — every cell renders its digit, and tones layer on a ring plus a fill so highlighted cells stay legible for colourblind users.
- The tone-change colour transition respects
prefers-reduced-motion: reduceand collapses to an instant swap when the user opts out.
Credits
- Extracted from:
algoflashcards(src/lessons/primitives/viz/BitRow.tsx). The library version drops the original's clickable rocker-switch interaction surface (consumers wire their own toggle behaviour around the read-only render), folds the multi-state palette into the four-tone semantic set used across DSA-viz siblings, and acceptsbigint/ binary-string inputs so visualisations beyond 32 bits work without a code change.