Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section2.3Unsigned Decimal to Binary Conversion

In Section 2.2, we covered conversion of a binary number to decimal. In this section we will learn how to convert an unsigned decimal integer to binary. Unsigned numbers have no sign, and signed numbers can be either positive or negative. Be careful to remember that a positive signed number is not unsigned.

Say we wish to convert a unsigned decimal integer, \(N\text{,}\) to binary. We set it equal to the expression in Equation (2.2.4), giving us: \begin{equation} N = d_{n-1} \times 2^{n-1} + d_{n-2} \times 2^{n-2} + \ldots + d_{1} \times 2^{1} + d_{0} \times 2^{0} \label{eq-dec2bin}\tag{2.3.1} \end{equation} where \(d_{i} = 0\) or \(1\text{.}\) Dividing both sides by \(2\text{,}\) \begin{equation} N_{1} + \frac{r_0}{2} = d_{n-1} \times 2^{n-2} + d_{n-2} \times 2^{n-3} + \ldots + d_{1} \times 2^{0} + d_{0} \times 2^{-1} \label{eq-divby2}\tag{2.3.2} \end{equation} where \(N_{1} = N/2\) (the integer div operation) and the remainder, \(r_0\text{,}\) is \(0\) or \(1\text{.}\) Since \(N_{1}\) is an integer and all the terms except the \(2^{-1}\) term on the right-hand side of Equation (2.3.2) are integers, we can see that \(d_{0} = r_{0}\text{.}\) Subtracting \(\frac{r_{0}}{2}\) from both sides gives, \begin{equation} N_{1} = d_{n-1} \times 2^{n-2} + d_{n-2} \times 2^{n-3} + \ldots + d_{1} \times 2^{0} \label{eq-divedby2}\tag{2.3.3} \end{equation} Dividing both sides of Equation (2.3.3) by two: \begin{equation} N_{2} + \frac{r_1}{2} = d_{n-1} \times 2^{n-3} + d_{n-2} \times 2^{n-4} + \ldots + d_{1} \times 2^{-1} \label{eq-divedby4}\tag{2.3.4} \end{equation} where \(N_{2} = N_{1}/2\text{.}\) From Equation (2.3.4) we see that \(d_{1} = r_{1}\text{.}\) It follows that the binary representation of a number can be produced from right (low-order bit) to left (high-order bit) by applying the algorithm shown in Algorithm 2.3.1.

There are times in some programs when it is more natural to specify a bit pattern rather than a decimal number. We have seen that it is possible to easily convert between the number bases, so you could convert the bit pattern to a decimal value, then use that. It is usually much easier to think of the bits in groups of four, then convert the pattern to hexadecimal.

For example, if your algorithm required the use of zeros alternating with ones, \begin{equation*} \binary{0101 0101 0101 0101 0101 0101 0101 0101} \end{equation*} this can be converted to the decimal value, \begin{equation*} 431655765 \end{equation*} or the hexadecimal value (shown here in C/C++ syntax)


Once you have memorized Table 2.1.1, it is clearly much easier to work with hexadecimal for bit patterns.

The discussion in these two sections has dealt only with unsigned integers. The representation of signed integers depends upon some architectural features of the CPU and will be discussed in Chapter 3 when we discuss computer arithmetic.



Convert \(123_{10}\) to binary.

Hint Answer

Convert the following unsigned decimal integers to 8-bit hexadecimal representation:

  1. \(100\)

  2. \(123\)

  3. \(10\)

  4. \(88\)

  5. \(255\)

  6. \(16\)

  7. \(32\)

  8. \(128\)


Convert the following unsigned decimal integers to 16-bit hexadecimal representation:

  1. \(1024\)

  2. \(1000\)

  3. \(32768\)

  4. \(32767\)

  5. \(256\)

  6. \(65535\)

  7. \(4660\)

  8. \(43981\)


Invent a code that would allow us to store letter grades with plus or minus. That is, the grades A, A-, B+, B, B-,…, D, D-, F. How many bits are required for your code?