Best afterDecimal to binary·Modular arithmetic: wrapping around
Overflow
A real computer stores a number in a fixed number of bits. Count past the largest value those bits can hold and there is nowhere for the carry to go — so it wraps around to zero. Python hides this from you, which is exactly why it catches people out. (This is not the same as roundoff — keep the two straight.)
Count upward in a fixed 8-bit register (it can only hold 0 to 255) and see what happens past the top.
| variable | value |
|---|---|
count | 254 |
notecount starts at 254. Pretend count lives in a fixed 8-bit register — it can only hold whole numbers from 0 to 255.
Click a line, drag the slider, or use the ← → keys.
What you are looking at
An 8-bit register holds whole numbers from 0 to 255 — that is all 8 bits can encode. We start at 254 and tick upward.
- Procedural — a simulated 8-bit register (
(count + 1) % 256). At255every bit is already a 1; one more tick wraps it to0. That wrap is overflow — the odometer rolling999 → 000. - Idiomatic — plain Python. Its integers have no fixed width, so
255becomes256,257, on and on. No wrap, because there is no register to overrun. Useful, but it can lull you into forgetting that real hardware does overflow.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.