How do you convert a decimal number (0-255) to an 8-bit binary number?
Walk the position values left to right (128 first); if the number is ≥ the value write 1 and subtract it, otherwise write 0. Example: 192 → 11000000.
You need this whenever you turn a dotted-decimal IPv4 address or a mask like 255.255.255.0 into the bit pattern a device actually uses — the basis of subnetting. The subtraction method is greedy: always take the largest position value that fits, so each decimal 0-255 produces exactly one 8-bit pattern. Gotcha: pad with leading zeros to a full 8 bits (e.g. 10 → 00001010, not 1010).
* Greedy subtraction: walk the weights left to right, take each that fits and subtract it, write 0 otherwise. *
Decimal to Binary Conversion (Subtraction Method):
- Start with position values: 128, 64, 32, 16, 8, 4, 2, 1
- Compare decimal to each position value (left to right)
- If decimal ≥ position value: write 1, subtract the value
- If decimal < position value: write 0, move to next position
Example: Convert 192 to binary
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| 192 ≥ 128? Yes | 1 | |||||||
| 64 ≥ 64? Yes | 1 | 1 | ||||||
| 0 ≥ 32? No | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
Result: 192 = 11000000
Go deeper:
Converting decimal numbers to binary — Khan Academy — the same largest-power-that-fits method, worked on screen.
RapidTables — decimal to binary converter — verify your by-hand result for any value 0-255.