Section2.3Unsigned Decimal to Binary Conversion¶ permalink

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}
\frac{N}{2} + \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 / is the div operator and the remainder, \(r_0\text{,}\) is \(0\) or \(1\text{.}\) Since \(\frac{N}{2}\) is an integer and all the terms except the \(2^{-1}\) term on the righthand 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}
\frac{N}{2} = 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}
\frac{N}{4} + \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}
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.

Algorithm2.3.1Convert Unsigned Decimal to Binary

Refer to Equation (2.3.1). / is the div operator and % is the mod operator.

\(quotient = N\)

\(i = 0\)

\(d_{i} = quotient\) % \(2\)

\(quotient = quotient / 2\)

While \(quotient \ne 0\)

\(i = i + 1\)

\(d_{i} = quotient\) % \(2\)

\(quotient = quotient / 2\)

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)

0x55555555

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.

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?