# Binary Conversion:

## Introduction

This subsection discusses the conversion process between decimal and binary numbers.

## Converting from Binary to Decimal

The conversion from binary is actually simpler than you might think at first. There are five main steps:

1. Start by placing a 20 above the rightmost bit.
2. Place 2's across the rest of the bits.
3. Starting from the right, place an exponent for each 2 so that it is counting up from 20
4. Now, multiply each bit by the power of 2 above it, i.e., 1 * 22.
5. Finally, to get the decimal equivalent, add all of the numbers from the previous step and that is the decimal equivalent.

Here is an example of the conversion of the binary number 1110 0010:

#### Step 1:

Place 20 above the right most bit

 20 1 1 1 0 0 0 1 0

#### Step 2:

Place 2's above all of the bits, like in step 1 (just without the exponent).

 2 2 2 2 2 2 2 20 1 1 1 0 0 0 1 0

#### Step 3:

Starting from the right, place exponents on the 2's so that it counts up from the right.

 27 26 25 24 23 22 21 20 1 1 1 0 0 0 1 0

#### Step 4:

Multiply each bit by the power of 2 above it.

 Powers of 2: 27 26 25 24 23 22 21 20 Bits: 1 1 1 0 0 0 1 0 Multiplied result: 27 26 25 0 0 0 21 0

#### Step 5:

Add all of the results from the previous step and the result will be the decimal equivalent.

$\text{Decimal Equivalent} = 2^7 + 2^6 + 2^5 + 2^1 = 128 + 64 + 32 + 2 = 226$

A table of powers of two can help with conversions and further cement that bit positions have solid values.

## Converting From Decimal to Binary

The conversion process from decimal to binary is a little more involved since the faster (more efficient) way requires the use of logarithms, but there is a simpler but more involved process where you look for the largest power of 2 in the number. The 8 steps for some decimal number $D$, using the logarithm method, are:

1. Take the $log_2$ of $D$.
2. Round the result from step 1 down, we will call this result $P$ and write this down in a separate area, since these values are the positions of the 1's.
3. Using the decimal number, subtract $2^P$ from it, this will be our new decimal number (i.e., $D_\text{new}=D-2^P$). Repeat steps 1 through 4 until $D=0$.
4. Now that you have all the powers of 2 that were in the original $D$, arrange them from left to right largest to smallest, with extra space for each number skipped.
5. Write 1's below the numbers and 0's below the extra space (remember there is a 0 bit position!).
6. The result should be the equivalent binary number (check with an online converter to be sure!).

An example conversion should help clear up how the process works. We will convert 192 into binary:

### First Iteration:

#### Step 1:

• Find $log_2 D$
• $log_2 192 = 7.58$

#### Step 2:

• Round the result in step 1 down, $P = 7$, and write it down nearby.
• $7$

#### Step 3:

• Now, subtract $2^P$ from our number and this resulting number will be our next starting number until the result is 0.
• $2^P = 2^7 = 128$
• $D = 192 - 128 = 64$

### Second Iteration:

#### Step 1:

• $log_2 64 = 6$

#### Step 2:

• $P = 6$
• $7,6$

#### Step 3:

• $2^6 = 64$
• $D = 64 -64 = 0$

#### Step 4:

Since $D$ is now $0$, we will arrange our written numbers from left to right, largest to smallest, and spaces for numbers skipped. (Remember that 0 should be included/is the last number).

 7 6

#### Step 5:

Write 1's below the values written in, and 0's for the blank spaces.

 7 6 1 1 0 0 0 0 0 0

#### Step 6:

The binary number is the sequence of bits you just filled in (in step 7). This result can be confirmed with an online binary converter. Such as the one here.

The other method of converting from decimal to binary is by finding the largest power of 2 in that number, and then subtracting it, and repeating until the result is 0. Recording each power until we stop. This is the more intuitive method that may be easier, especially with a table of the powers of 2 to help speed the process up. Like the table above(which only goes up to $2^8$).

The main process of this method is as follows:

1. Find the highest power of 2.
2. Subtract it from the current number.
3. Write down the power of 2 used. Repeat steps 1 through 3 until the number is 0.
4. Like the previous method, write the numbers out left to right, highest to smallest, leaving spaces between powers of 2.
5. Write 1's under the powers of 2 written in, and 0's under the blanks, remember that 0 is a power of 2, and the result will be the binary number. This can be confirmed in the link given in the previous example.

#### Step 1:

Using the previous example number of 192, we will find the highest power of 2 that fit into it. This power of 2 is $2^7$, which is 128.

#### Step 2:

We subtract the current number, 192, by the power of 2 we found to get our next current number.

$192-128=64$

#### Step 3:

Write down the power of 2 we used and go back to step 1 until we hit 0.

$7$

#### Step 1 (Second run through):

We find that the highest power of 2 that fits into 64, is 64, which is $2^6$.

#### Step 2:

We subtract our numbers to get the next number.

$64-64=0$

#### Step 3:

We write down the power of 2, and break out of our loop since we hit 0.

$7 6$

#### Step 4:

Write out the powers of two we used, leaving blanks for the numbers we didn't use.

 $7$ $6$

#### Step 5:

Write 1's under the powers of 2 that are filled in, and 0's under the blank spaces.

 $7$ $6$ 1 1 0 0 0 0 0 0

This number is 1100 0000 and can be confirmed by the process we used above, and in the binary converter that is also linked above as well.