Section 2.5 Unsigned Decimal to Binary Conversion
Signed numbers can be either positive or negative, but unsigned numbers have no sign. Be careful to remember that a positive signed number is not unsigned.
Say we wish to convert an unsigned decimal integer, \(N\text{,}\) to binary. We set it equal to the expression in Equation (2.3.4), giving us:
where \(d_{i} = 0\) or \(1\text{.}\) Dividing both sides by \(2\text{,}\)
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 righthand side of Equation (2.5.2) are integers, we can see that \(d_{0} = r_{0}\text{.}\) Subtracting \(\frac{r_{0}}{2}\) from both sides gives,
Dividing both sides of Equation (2.5.3) by two:
where \(N_{2} = N_{1}/2\text{.}\) From Equation (2.5.4) we see that \(d_{1} = r_{1}\text{.}\) It follows that the binary representation of a number can be produced from right (loworder bit) to left (highorder bit) by applying the algorithm shown in Algorithm 2.5.1.
Algorithm 2.5.1. Convert Unsigned Decimal to Binary.
Refer to Equation (2.5.1). /
is the div
operator and %
is the mod
operator.
\(\displaystyle quotient = N\)
\(\displaystyle i = 0\)
\(d_{i} = quotient\) % \(2\)
\(quotient = quotient\) / \(2\)

While \(quotient \ne 0\)
\(\displaystyle 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, thus you could convert the bit pattern to a decimal value and then use that. But it is usually much easier to think of the bits in groups of four and use hexadecimal to specify each group.
For example, if your algorithm required the use of zeros alternating with ones,
this can be converted to the decimal value,
or expressed in hexadecimal (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.