From decimal to binary by dividing by 2
To write a decimal number in binary, divide it by 2 and write down the quotient and the remainder (0 or 1). Then divide the quotient by 2, and go on until the quotient is 0. The remainders are the bits of the answer, but they come out in the wrong order: the first remainder belongs to the place worth 1, so it is the last bit on the right. Read the remainders from the last one to the first, that is, from the bottom of your table to the top. In the table, 45 gives the remainders 1, 0, 1, 1, 0, 1 from top to bottom, so (45)₁₀ = (101101)₂. A quick check: 32 + 8 + 4 + 1 = 45.
| Division | Quotient | Remainder |
|---|---|---|
| 45 ÷ 2 | 22 | 1 |
| 22 ÷ 2 | 11 | 0 |
| 11 ÷ 2 | 5 | 1 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
| Division | Quotient | Remainder |
|---|---|---|
| 29 ÷ 2 | 14 | 1 |
| 14 ÷ 2 | 7 | 0 |
| 7 ÷ 2 | 3 | 1 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
1<?php 2$n = 29; 3$bits = ""; 4while ($n > 0) { 5 $bits = ($n % 2) . $bits; 6 $n = intdiv($n, 2); 7} 8echo $bits;
11101
Choose three decimal numbers between 10 and 50. Convert each to binary by repeated division by 2, writing the table on paper. Convert every answer back with place values to check it.