Skip to the content of the web site.

2.2 Binary Numbers

Introduction Theory HOWTO Examples Questions Matlab Maple

Introduction

It is very easy for a computer to represent two separate states by using two voltages (e.g., 0 V and 5 V, usually called low and high, respectively). There are numerous engineering problems with using a greater number of levels to represent more bits, and therefore it is much easier to use a number system which uses only two symbols instead of ten.

We will look at the representation and the conversion of a binary number to a decimal number.

For each problem with binary representation arithmetic, there is a corresponding problem with decimal numbers, though the second is much more complex.

Background

Useful background for this topic includes:

References

Theory

Assumptions

Instead of using ten unique digits to represent numbers, for binary representations we use only two: 0 and 1. The word bit is used as an abbreviation for a binary digit, and thus we will refer to 0 or 1 as bits when we discuss binary numbers.

Binary Integers

To begin, we can consider the process of counting in binary: each time we increment an integer, we add 1 to the least significant bit and if the sum is greater than 1, we increment the next highest bit, possibly going on in this manner:

            1
           10
           11
          100
          101
          110
          111
         1000
         1001
         1010
         1011
         1100

Thus, the first number represents the decimal integer 1, the second represents the decimal integer 2, and so on. As well as calling these binary numbers, another term used to describe such numbers is to state that they are in base two (or base 2). The corresponding values are shown in Table 1.

Table 1. Binary and decimal equivalent numbers.

BinaryDecimal
11
102
113
1004
1015
1106
1117
10008
10019
101010
101111
110012

Distinguishing Binary Numbers

One problem is distinguishing between a binary 10 (= 2)and a decimal 10. When the context is not obvious, we suffix any decimal integer with a subscript 10 (to represent base 10) and any binary number with a subscript 2 (to represent base 2). For example, 1010 = 10102 and 563210 = 10110000000002.

Binary Number Representation

We use two bits, 0 and 1, to represent numbers. An (n + 1)-bit binary integer is a number of the form bn⋅⋅⋅b2b1b0 where each bi is a bit and bn = 1. This represents the number

When a bit bi = 1 is incremented, it is replaced by 0 and the bit bi + 1 is incremented by 1.

Using these definitions, we derive the basic rules for binary addition and multiplication.

We may generalize this to represent arbitrary real numbers by including a radix point (after all, we cannot call it a decimal point anymore) and allowing numbers of the form bn⋅⋅⋅b0.b-1b-2⋅⋅⋅ which represents the number

In general, we will truncate such a number to the form:

where m < n.

Again, using this definition of a real number, we may derive rules for binary addition and multiplication.

Conversion from Binary to Decimal

From the definition, we may simply calculate the sum. For example, the integer 110102 represents 1⋅16 + 1⋅8 + 0⋅4 + 1⋅2 + 0⋅1 = 16 + 8 + 4 = 28. The real number 1.011 represents 1⋅1 + 0⋅0.5 + 1⋅0.25 + 1⋅0.125 = 1 + 0.25 + 0.125 = 1.375 .

Conversion from Decimal to Binary (for information only)

This is a little more tedious and there is nothing insightful to be gained from memorizing such an algorithm. If it is a decimal integer, keep dividing by 2 and keep track the remainders. If it is a real number x, find the largest power n of 2 such that 2-nx < 1 and keep track of whether, by multiplying successively by two whether or not the integer part is 0 or 1 and discarding the integer part with each step.

HOWTO

Problem

Given two binary integers and two binary numbers, add and multiply them.

Assumptions

For binary numbers, we will assume that the number of bits is finite. These rules can be extended for an infinite number of bits, but this is unnecessary.

Binary Integer Addition

Right-justifying both binary integers and add each column, starting from the right at the least-significant bit and moving left. Recall that 1 + 1 = 10, and therefore, if a column sums to more than 1, write down the least-significant bit and carry the remaining bits to the next column. Continue until all columns, including any carries, have been added.

For example, 1011102 + 111012 is calculated as follows.

               1111
                101110
              +  11101
                ------
               1001011

Binary Integer Multiplication

Given two binary integers, starting with the least significant bit b0 of the second integer, multiply the first integer by that bit. Next, for each higher bit bi in the second integer, multiply the first integer by bi⋅10i. Finally, sum these products.

For example, 1011102 × 111012 is calculated as follows.

         101110
       x  11101
         ------
      111
    1100011          carry for sum: read numbers vertically
         101110      101110 × 1
        0000000      101110 × 00
       10111000      101110 × 100
      101110000      101110 × 1000
   + 1011100000      101110 × 10000
     ----------
    10100110110

Note that the zeros are often omitted for convenience:

         101110
       x  11101
         ------
      111            
    1100011          
         101110      
        000000       ←
       101110        ←
      101110         ←
   + 101110          ←
     ----------
    10100110110

Binary Number Addition

Line up the radix points and add each column, starting from the right and moving left. If a bit is missing, assume it is zero. If a column sums to more than 1, write down the least-significant bit and carry the remaining bits to the next column. Continue until all columns, including any carries, have been added.

For example, 110010.100011 + 11011.00101 is calculated as follows:

     11  1      1
      110010.100011
   +   11011.00101
     --------------
     1001101.101101

Binary Number Multiplication

Given two binary numbers, total the number of bits to the right of the radix points. Next, remove the radix points and multiply the two numbers as binary integers. Starting with the least significant bit b0 of the second integer, multiply the first integer by that bit. Next, for each higher bit bi in the second integer, multiply the first integer by bi⋅102i. Finally, sum these products.

For example, 11.01 × 10.101 has five bits to the right of the radix points and thus we calculate:

           1101
        x 10101
          -----
      111111       carry for sum
           1101
          00000
         110100
        0000000
     + 11010000
      ---------
      100010001

Adding back 5 bits to the right of the radix point, we get 1000.10001.

Note that the zeros are often omitted for convenience:

           1101
        x 10101
          -----
      111111       carry for sum
           1101
          0000 
         1101  
        0000   
     + 1101    
      ---------
      100010001

Conversion from Binary to Decimal

Given the binary number bn⋅⋅⋅b0.b-1b-2⋅⋅⋅, we need only calculate the sum

to determine the decimal value. For example, 101.1012 represents the real number 1⋅22 + 0⋅21 + 1⋅20 + 1⋅2-1 + 0⋅2-2 + 1⋅2-3 = 4 + 1 + 0.5 + 0.125 = 5.625 .

Examples

1. Add the binary integers 1011012 and 101012.

    1111 1         carry
     101101
   +  10101
     -------
    1000010

2. Add the binary numbers 100.0110112 and 10.101112.

     1 1111        carry
   100.011011
  + 10.10111
   ----------
   111.001001

Lining up the radix points, each column is added, recalling that 1 + 1 = 0 with a carry of 1, and 1 + 1 + 1 = 1 with a carry of 1.

3. Multiply the binary integers 10112 and 1012.

        1011
      ×  101
        ----
       1           carry for sum
        1011
       0000
    + 1011
      ------
      110111

Note that there cannot be any carries in the multiplication: you either have 0 × 1011 or 1 × 1011, a much simpler rule than that for decimal multiplication.

4. Multiply the binary numbers 10.11012 and 1.0112.

        10.1101
          1.011
        -------
       1111     carry for sum
         101101
        101101
       000000
    + 101101
      ---------
      111101111

Adding the number of digits beyond the radix points (4 + 3 = 7), we offset the radix point in the answer: 11.11011112.

5. Convert the binary number 10011.011012 from binary to decimal.

This number represents:

1⋅24 + 0⋅23 + 0⋅22 + 1⋅21 + 1⋅20 + 0⋅2-1 + 1⋅2-2 + 1⋅2-3 + 0⋅2-4 + 1⋅2-5 which equals 16 + 2 + 1 + 0.25 + 0.125 + 0.03125 = 19.40625 .

Questions

1. Add the two binary integers 1001112 and 10001102. (11011012)

2. Add the two binary integers 11000112 and 101001011012. (101100100002)

3. Add the two binary numbers 100.1112 and 10.001102. (111.00012)

4. Add the two binary numbers 110.00112 and 10100.1011012. (11010.1110012)

5. Multiply the two binary integers 1001112 and 10102. (1100001102)

6. Multiply the two binary integers 11000112 and 100112. (111010110012)

7. Multiply the two binary numbers 100.1112 and 10.102. (1100.00112)

8. Multiply the two binary numbers 1100.0112 and 10.0112. (11101.0110012)

9. Convert the binary number 10010.00112 to decimal. (18.1875)

10. Convert the binary number -111.1111112 to decimal. (-7.984375)

11. Using a geometric sum, what is the binary number 0.1111111⋅⋅⋅2 equal to? (1)

Matlab

Matlab uses binary to store its numbers, though it is difficult to view numbers in their (non-floating-point representation) binary form.

Maple

You can convert a decimal number into its binary representation in Maple:

convert( 352235, binary );                # convert to binary
convert( 10101011, decimal, binary );     # convert to decimal from binary