Best afterDecimal to binary
Binary to decimal
Going from binary back to decimal is one loop: read the bits left to right, and at each step double what you have and add the next bit. The whole lesson hides in one number — the base you multiply by. Get it wrong and 1101 comes out as eleven hundred and one instead of thirteen.
Read a binary number left to right and work out the decimal value it represents.
Binary to decimal — reading it as base ten execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
bits | [1,1,0,1] |
step 1 / 11
Click a line, drag the slider, or use the ← → keys.
What you are looking at
The bits are 1 1 0 1. In binary the places are 8 4 2 1, so this is 8 + 4 + 0 + 1 = 13. The loop builds that up by doubling: 0 → 1 → 3 → 6 → 13.
- Buggy —
value * 10 + bmultiplies by ten, treating the bits as decimal digits. value climbs1 → 11 → 110 → 1101— it just reassembles the digits as a base-ten number, not the value the binary actually represents. - Procedural —
value * 2 + bdoubles, which is what shifting up one binary place means. value climbs1 → 3 → 6 → 13and lands on the right answer.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.