Decimal to Binary Recursive Converter
Convert decimal numbers to binary using a step-by-step recursive algorithm. Visualize each recursion level with remainder tracking and full bit output.
| Depth | Call | n ÷ 2 | Quotient | Remainder (bit) |
|---|
About
Converting a decimal integer to its binary representation is a fundamental operation in computer science, underpinned by repeated Euclidean division by 2. The recursive approach mirrors the mathematical definition directly: the binary representation of n is the binary representation of n รท 2 (integer quotient) concatenated with n mod 2. Each recursion level extracts one bit. The base case fires when the quotient reaches 0. An incorrect implementation silently drops leading zeros or mishandles the base case, producing wrong results that propagate through downstream bitwise operations, protocol encodings, or hardware register mappings.
This tool executes real recursive division and exposes every stack frame. It handles non-negative integers up to 253 โ 1 (the JavaScript safe integer boundary defined by IEEE 754 double-precision). For negative inputs, two's complement encoding is applied at 8, 16, or 32-bit widths. The tool approximates nothing. Every displayed bit is computed, not looked up.
Formulas
The recursive decimal-to-binary conversion rests on the identity that any non-negative integer n in base 10 can be decomposed by repeated division by 2.
Where n = the input decimal integer, โ โ = floor division (integer quotient), mod = modulo operator returning the remainder. The recursion depth equals โlog2(n)โ + 1 for n > 0. The total number of bits in the binary output equals the recursion depth. For negative numbers, two's complement is computed as 2w + n where w is the bit width (8, 16, or 32).
Reference Data
| Decimal | Binary | Hex | Octal | Bits Required | Recursion Depth |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 | 2 | 2 |
| 7 | 111 | 7 | 7 | 3 | 3 |
| 8 | 1000 | 8 | 10 | 4 | 4 |
| 10 | 1010 | A | 12 | 4 | 4 |
| 15 | 1111 | F | 17 | 4 | 4 |
| 16 | 10000 | 10 | 20 | 5 | 5 |
| 42 | 101010 | 2A | 52 | 6 | 6 |
| 98 | 1100010 | 62 | 142 | 7 | 7 |
| 107 | 1101011 | 6B | 153 | 7 | 7 |
| 127 | 1111111 | 7F | 177 | 7 | 7 |
| 128 | 10000000 | 80 | 200 | 8 | 8 |
| 255 | 11111111 | FF | 377 | 8 | 8 |
| 256 | 100000000 | 100 | 400 | 9 | 9 |
| 1000 | 1111101000 | 3E8 | 1750 | 10 | 10 |
| 1023 | 1111111111 | 3FF | 1777 | 10 | 10 |
| 1024 | 10000000000 | 400 | 2000 | 11 | 11 |
| 4096 | 1000000000000 | 1000 | 10000 | 13 | 13 |
| 65535 | 1111111111111111 | FFFF | 177777 | 16 | 16 |