Last updated
The Division-by-2 Method
The classic way to convert a decimal integer to binary by hand is repeated division by 2. Divide the number by 2, record the remainder (0 or 1), then divide the quotient again. Read the remainders from bottom to top to get the binary representation.
Convert 90 to binary:
90 ÷ 2 = 45 remainder 0
45 ÷ 2 = 22 remainder 1
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Read remainders bottom to top: 1011010
Verify: 64+16+8+2 = 90 ✓
Number Base Systems Compared
| Base | Name | Digits | Prefix | Use case |
|---|---|---|---|---|
| 2 | Binary | 0, 1 | 0b | CPU instructions, bitwise ops |
| 8 | Octal | 0–7 | 0o | Unix permissions (chmod) |
| 10 | Decimal | 0–9 | (none) | Human-readable numbers |
| 16 | Hexadecimal | 0–9, A–F | 0x | Memory addresses, colors, hashes |
Why Hexadecimal Is Preferred Over Binary
Binary is verbose — the number 255 requires 8 digits (11111111).
Hexadecimal is a compact shorthand: each hex digit represents exactly 4 binary bits (a nibble).
So 0xFF = 1111 1111 = 255. This is why memory addresses, color codes,
and hash values are written in hex rather than binary.
Converting between binary and hex is trivial: group binary digits in sets of 4 from the right, then replace each group with its hex equivalent:
Binary: 1010 1111 0011 0101
Hex: A F 3 5
Result: 0xAF35
Floating-Point Binary
Decimal fractions don't always convert cleanly to binary. The number 0.1 in decimal
is a repeating fraction in binary: 0.0001100110011... (infinite).
This is why 0.1 + 0.2 !== 0.3 in JavaScript and most other languages —
both values are approximations in IEEE 754 double-precision floating-point format.
Use Number.EPSILON comparisons or a decimal library when exact decimal arithmetic matters.