☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Binary and decimal numbers

الأسبوع 3 · الوحدة ١ «النظم العددية» (٣): التحويل من الثنائي إلى العشري ومن العشري إلى الثنائي · 3 دروس
2

From decimal to binary by dividing by 2

ICT Secondary 3 Term 1, Unit 1, Topic 1: pp. 14–17
GoalTurn a decimal number into binary by repeated division by 2.
New words
quotient · the whole-number result of a divisionremainder · what is left after a division; when dividing by 2 it is 0 or 1repeated division · dividing each new quotient by 2 again until the quotient is 0
Explanation

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.

DivisionQuotientRemainder
45 ÷ 2221
22 ÷ 2110
11 ÷ 251
5 ÷ 221
2 ÷ 210
1 ÷ 201
Worked examples
DivisionQuotientRemainder
29 ÷ 2141
14 ÷ 270
7 ÷ 231
3 ÷ 211
1 ÷ 201
Convert 29 to binary. Divide by 2 until the quotient is 0 and read the remainders from bottom to top.
Php
1<?php
2$n = 29;
3$bits = "";
4while ($n > 0) {
5    $bits = ($n % 2) . $bits;
6    $n = intdiv($n, 2);
7}
8echo $bits;
Output
11101
A program can do the same work. The symbol % gives the remainder, and each new remainder is put in front of the bits found so far, so the bits are already in the right order at the end.
Class activity

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.

Practice
1
Convert 13 to binary.
2
Convert 40 to binary.
3
Why do we stop dividing when the quotient is 0?
4
Dividing a number repeatedly by 2 gives the remainders 0, 1, 1 in this order. Which binary number is it?