Converting decimal to binary string

As we have discussed in the previous section, the binary string of 9 is 1001. We can convert a decimal number into a binary digit by dividing the number by 2 until it cannot be divided any more, and collect the remainder of each division. Here's the steps to convert 9 into binary digit:

9 is divided by 2 is 4, remainder is 14 is divided by 2 is 2, remainder is 02 is divided by 2 is 1, remainder is 01 is divided by 2 is 0, remainder is 1

Look at the remainder of each division, and read it from the bottom to the top. So that's why we have 1001 as a binary digit of 9. Now, let's try another larger number, which is 500. Here are the steps to convert 500 to binary digit:

500 is divided by 2 is 250, remainder ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.