Miscellaneous problems · Exercise 77 of 100

Convert decimal to binary

For a positive whole number, divide by two, keep the remainder, and repeat with the whole-number quotient until it reaches zero. Each remainder gives the rightmost digit still to be found, so read the remainders in reverse order. Zero itself is written as 0.

Try it yourself

A counter displays 22 in decimal. What binary numeral represents the same count without leading zeros?

One-step hint

Write descending powers of two starting at 16. At each power, write 1 and subtract it if it fits the remaining count, or write 0 if it doesn't.

Worked solution
  1. Dividing 22 by two gives 11 with remainder 0. Dividing 11 gives 5 with remainder 1, and dividing 5 gives 2 with remainder 1.
  2. Dividing 2 and then 1 gives the last two remainders, 0 and 1. Reading all five remainders backward gives 10110.
DSA compiler