Skip to main content

Section 1.4 Binary Representation of Positive Integers

Subsection 1.4.1 Grouping by Twos

Recall that the set of positive integers, \(\mathbb{P}\text{,}\) is \(\{1, 2, 3, . . . \}\text{.}\) Positive integers are naturally used to count things. There are many ways to count and many ways to record, or represent, the results of counting. For example, if we wanted to count five hundred twenty-three apples, we might group the apples by tens. There would be fifty-two groups of ten with three single apples left over. The fifty-two groups of ten could be put into five groups of ten tens (hundreds), with two tens left over. The five hundreds, two tens, and three units is recorded as 523. This system of counting is called the base ten positional system, or decimal system. It is quite natural for us to do grouping by tens, hundreds, thousands, \(\dots\) since it is the method that all of us use in everyday life.

The term positional refers to the fact that each digit in the decimal representation of a number has a significance based on its position. Of course this means that rearranging digits will change the number being described. You may have learned of numeration systems in which the position of symbols does not have any significance (e.g., the ancient Egyptian system). Most of these systems are merely curiosities to us now.

The binary number system differs from the decimal number system in that units are grouped by twos, fours, eights, etc. That is, the group sizes are powers of two instead of powers of ten. For example, twenty-three can be grouped into eleven groups of two with one left over. The eleven twos can be grouped into five groups of four with one group of two left over. Continuing along the same lines, we find that twenty-three can be described as one sixteen, zero eights, one four, one two, and one one, which is abbreviated \(10111_{\textrm{two}}\text{,}\) or simply \(10111\) if the context is clear.

Subsection 1.4.2 A Conversion Algorithm

The process that we used to determine the binary representation of \(23\) can be described in general terms to determine the binary representation of any positive integer \(n\text{.}\) A general description of a process such as this one is called an algorithm. Since this is the first algorithm in the book, we will first write it out using less formal language than usual, and then introduce some “algorithmic notation.” We will cover algorithms specifically in Chapter 9

  1. Start with an empty list of bits.

  2. Step Two: Assign the variable \(k\) the value \(n\text{.}\)

  3. Step Three: While \(k\)'s value is positive, continue performing the following three steps until \(k\) becomes zero and then stop.

    1. divide \(k\) by 2, obtaining a quotient \(q\) (often denoted \(k \textrm{ div } 2\)) and a remainder \(r\) (denoted \((k \bmod 2)\)).

    2. attach \(r\) to the left-hand side of the list of bits.

    3. assign the variable \(k\) the value \(q\text{.}\)

To determine the binary representation of 41 we take the following steps:

  • \(41 = 2 \times 20+ 1 \quad List = 1 \)

  • \(20 = 2 \times 10+0 \quad List = 01 \)

  • \(10 = 2\times 5 + 0 \quad List = 001 \)

  • \(5 =\text2\times 2+ 1 \quad List =1001\)

  • \(2 =2\times 1+ 0 \quad List = 01001 \)

  • \(1 =\text2 \times 0\text+1 \quad List = 101001\)

Therefore, \(41=101001_{\textrm{two}}\)

The notation that we will use to describe this algorithm and all others is called pseudocode, an informal variation of the instructions that are commonly used in many computer languages. Read the following description carefully, comparing it with the informal description above. Chapter 9, which contains a general discussion of the components of the algorithms in this book, should clear up any lingering questions. Anything after // are comments.

Subsection 1.4.3 SageMath Note: Binary Conversion Algorithm

Here is a Sage version of the algorithm with two alterations. It outputs the binary representation as a string, and it handles all integers, not just positive ones.

Now that you've read this section, you should get this joke.

Comic from http://xkcd.com.
Figure 1.4.3. With permission from Randall Munroe http://xkcd.com

Exercises 1.4.4 Exercises for Section 1.4

1.

Find the binary representation of each of the following positive integers by working through the algorithm by hand. You can check your answer using the sage cell above.

  1. 31

  2. 32

  3. 10

  4. 100

Answer
  1. \(11111\)

  2. \(100000\)

  3. \(1010\)

  4. \(1100100\)

2.

Find the binary representation of each of the following positive integers by working through the algorithm by hand. You can check your answer using the sage cell above.

  1. 64

  2. 67

  3. 28

  4. 256

3.

What positive integers have the following binary representations?

  1. 10010

  2. 10011

  3. 101010

  4. 10011110000

Answer
  1. \(18\)

  2. \(19\)

  3. \(42\)

  4. \(1264\)

4.

What positive integers have the following binary representations?

  1. 100001

  2. 1001001

  3. 1000000000

  4. 1001110000

5.

The number of bits in the binary representations of integers increases by one as the numbers double. Using this fact, determine how many bits the binary representations of the following decimal numbers have without actually doing the full conversion.

  1. 2017

  2. 4000

  3. 4500

  4. \(2^{50}\)

Answer

There is a bit for each power of 2 up to the largest one needed to represent an integer, and you start counting with the zeroth power. For example, 2017 is between \(2^{10}=1024\) and \(2^{11}=2048\text{,}\) and so the largest power needed is \(2^{10}\text{.}\) Therefore there are \(11\) bits in binary 2017.

  1. \(11\)

  2. \(12\)

  3. \(13\)

  4. 51

6.

Let \(m\) be a positive integer with \(n\)-bit binary representation: \(a_{n-1}a_{n-2}\cdots a_1a_0\) with \(a_{n-1}=1\) What are the smallest and largest values that \(m\) could have?

7.

If a positive integer is a multiple of 100, we can identify this fact from its decimal representation, since it will end with two zeros. What can you say about a positive integer if its binary representation ends with two zeros? What if it ends in \(k\) zeros?

Answer

A number must be a multiple of four if its binary representation ends in two zeros. If it ends in \(k\) zeros, it must be a multiple of \(2^k\text{.}\)

8.

Can a multiple of ten be easily identified from its binary representation?