D.5 Converting from Decimal to Binary, Octal or Hexadecimal
The conversions in Section D.4 follow naturally from the positional notation conventions. Converting from decimal to binary, octal, or hexadecimal also follows these conventions.
Suppose we wish to convert decimal 57 to binary. We begin by writing the positional values of the columns right to left until we reach a column whose positional value is greater than the decimal number. We do not need that column, so we discard it. Thus, we first write:
Positional values: | 64 |
32 |
16 |
8 |
4 |
2 |
1 |
Then we discard the column with positional value 64, leaving:
Positional values: | 32 |
16 |
8 |
4 |
2 |
1 |
Next we work from the leftmost column to the right. We divide 32 into 57 and observe that there ...
Get C++ How to Program, 10/e 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.