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
*bi*nary digi*t*, 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.

Binary | Decimal |

1 | 1 |

10 | 2 |

11 | 3 |

100 | 4 |

101 | 5 |

110 | 6 |

111 | 7 |

1000 | 8 |

1001 | 9 |

1010 | 10 |

1011 | 11 |

1100 | 12 |

# 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,
10_{10} = 1010_{2} and 5632_{10} = 1011000000000_{2}.

# 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 *b*_{n}⋅⋅⋅*b*_{2}*b*_{1}*b*_{0}
where each *b*_{i} is a bit and *b*_{n} = 1. This
represents the number

When a bit *b*_{i} = 1 is incremented, it is replaced by
0 and the bit *b*_{i + 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
*b*_{n}⋅⋅⋅*b*_{0}.*b*_{-1}*b*_{-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
11010_{2} 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^{-n}*x* < 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, 101110_{2} + 11101_{2} is calculated
as follows.

1111
101110
+ 11101
------
1001011

# Binary Integer Multiplication

Given two binary integers, starting with the least significant bit *b*_{0} of the
second integer, multiply the first integer by that bit. Next, for each
higher bit *b*_{i} in the second integer, multiply
the first integer by *b*_{i}⋅10^{i}.
Finally, sum these products.

For example, 101110_{2} × 11101_{2} 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 *b*_{0} of the
second integer, multiply the first integer by that bit. Next, for each
higher bit *b*_{i} in the second integer, multiply
the first integer by *b*_{i}⋅10_{2}^{i}.
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 *b*_{n}⋅⋅⋅*b*_{0}.*b*_{-1}*b*_{-2}⋅⋅⋅,
we need only calculate the sum

to determine the decimal value. For example, 101.101_{2} represents the real number
1⋅2^{2} + 0⋅2^{1} + 1⋅2^{0} + 1⋅2^{-1} + 0⋅2^{-2} + 1⋅2^{-3} = 4 + 1 + 0.5 + 0.125 = 5.625 .

# Examples

1. Add the binary integers 101101_{2} and 10101_{2}.

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

2. Add the binary numbers 100.011011_{2} and 10.10111_{2}.

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 1011_{2} and 101_{2}.

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.1101_{2} and 1.011_{2}.

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.1101111_{2}.

5. Convert the binary number 10011.01101_{2} from binary to decimal.

This number represents:

1⋅2

^{4} + 0⋅2

^{3} + 0⋅2

^{2} + 1⋅2

^{1} + 1⋅2

^{0} + 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 100111_{2} and 1000110_{2}. (1101101_{2})

2. Add the two binary integers 1100011_{2} and 10100101101_{2}. (10110010000_{2})

3. Add the two binary numbers 100.111_{2} and 10.00110_{2}. (111.0001_{2})

4. Add the two binary numbers 110.0011_{2} and 10100.101101_{2}. (11010.111001_{2})

5. Multiply the two binary integers 100111_{2} and 1010_{2}. (110000110_{2})

6. Multiply the two binary integers 1100011_{2} and 10011_{2}. (11101011001_{2})

7. Multiply the two binary numbers 100.111_{2} and 10.10_{2}. (1100.0011_{2})

8. Multiply the two binary numbers 1100.011_{2} and 10.011_{2}. (11101.011001_{2})

9. Convert the binary number 10010.0011_{2} to decimal. (18.1875)

10. Convert the binary number -111.111111_{2} 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