Conversion

ShareIn this tutorial we will learn to convert a decimal number which has fractional part into binary number.

Before we dive into the main topic lets talk a little about Decimal and Binary Number System that we are going to work with in this tutorial.

A decimal number system consists of 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. So, any number that we use in our daily life is actually in decimal number system.

A binary number system consists of only 2 digits: 0 and 1. And it is most commmonly used in computers.

A floating point decimal number consists of two parts. An integer part which is to the left of the decimal point and a fractional part which is to the right of the decimal point. For example, 10.16 is a floating point decimal number. The integer part of this number is 10 and the fractional part of the number is 0.16 and together they make up the number.

So, to convert a floating point decimal number into binary form we have to first convert the integer part into binary form. Then covert the fractional part into binary form. Finally combine the two to get the result.

To get the binary of the fractional part we have to multiple the fractional part by 2 and take the integer part before the decimal point as result and multiple the remaining fractional part by 2 again. We perform this process till the fractional part becomes 0. In some cases the fractional part will not become 0 so, for those scenarios we will stop after N digits, where N will be sufficiently large or given in the question.

First we convert the integer part 0 into binary and then the fractional part .125 into binary.

Integer part is 0 which is less than 2 so, 0_{(base 10)} = 0_{(base 2)}

```
Binary of 0.125
Step 1
----------
We multiply 0.125 by 2 and take the integer part
0.125 x 2 = 0.250
Integer part = 0
Fractional part = 0.250
As, fractional part is not equal to 0 so we copy it to next step.
Step 2
----------
We multiply 0.250 by 2 and take the integer part
0.250 x 2 = 0.500
Integer part = 0
Fractional part = 0.500
As, fractional part is not equal to 0 so we copy it to next step.
Step 3
----------
We multiply 0.500 by 2 and take the integer part
0.500 x 2 = 1.000
Integer part = 1
Fractional part = 0
Now the fractional part is 0 so, we stop here.
```

The calculated integer part are as followed.

Step 1: 0

Step 2: 0

Step 3: 1

To find the binary we have to scan the integer part from top

So, 0.125_{(base 10)} = 0.001_{(base 2)}

Alternatively, (0.125)_{10} = (0.001)_{2}

Where, (base 10) means the number is in decimal number system and (base 2) means the number is in binary number system.

First we convert the integer part 10 into binary.

```
Step 1
----------------
Dividend = 10
Divisor = 2
Quotient = 5
2 ) 10 ( 5
10
----
0
Dividing 10 by 2 we will get 0 as remainder.
So, 1st remainder = 0
Dividend for step 2 = quotient of step 1
So, dividend for step 2 = 5
Step 2
----------------
Dividend = 5
Divisor = 2
Quotient = 2
2 ) 5 ( 2
4
----
1
Dividing 5 by 2 we will get 1 as remainder.
So, 2nd remainder = 1
Dividend for step 3 = quotient of step 2
So, dividend for step 3 = 2
Step 3
----------------
Dividend = 2
Divisor = 2
Quotient = 1
2 ) 2 ( 1
2
----
0
Dividing 2 by 2 we will get 0 as remainder.
So, 3rd remainder = 0
Dividend for step 4 = quotient of step 3
So, dividend for step 4 = 1
Step 4
-----------------
Dividend = 1
As, dividend is less than 2 so, we will stop here and copy the dividend as the last remainder.
So, 4th remaider = 1
```

The calculated remainder are as followed.

1st remainder = 0

2nd remainder = 1

3rd remainder = 0

4th remainder = 1

To find the binary we have to scan the remainder from bottom.

So, 10_{(base 10)} = 1010_{(base 2)}

Alternatively, (10)_{10} = (1010)_{2}

Where, (base 10) means the number is in decimal number system and (base 2) means the number is in binary number system.

Now, we will convert the fractional part 0.16 into binary.

```
Binary of 0.16
Step 1
----------
We multiply 0.16 by 2 and take the integer part
0.16 x 2 = 0.32
Integer part = 0
Fractional part = 0.32
As, fractional part is not equal to 0 so we copy it to next step.
Step 2
----------
We multiply 0.32 by 2 and take the integer part
0.32 x 2 = 0.64
Integer part = 0
Fractional part = 0.64
As, fractional part is not equal to 0 so we copy it to next step.
Step 3
----------
We multiply 0.64 by 2 and take the integer part
0.64 x 2 = 1.28
Integer part = 1
Fractional part = 0.28
As, fractional part is not equal to 0 so we copy it to next step.
Step 4
----------
We multiply 0.28 by 2 and take the integer part
0.28 x 2 = 0.56
Integer part = 0
Fractional part = 0.56
As, fractional part is not equal to 0 so we copy it to next step.
Step 5
----------
We multiply 0.56 by 2 and take the integer part
0.56 x 2 = 1.12
Integer part = 1
Fractional part = 0.12
As, fractional part is not equal to 0 so we copy it to next step.
Step 6
----------
We multiply 0.12 by 2 and take the integer part
0.12 x 2 ...
in this case, we have 5 digits as answer and the fractional part is still not 0 so, we stop here.
```

The calculated integer part are as followed.

Step 1: 0

Step 2: 0

Step 3: 1

Step 4: 0

Step 5: 1

...

To find the binary we have to scan the integer part from top

So, 0.16_{(base 10)} = 0.00101..._{(base 2)}

Alternatively, (0.16)_{10} = (0.00101...)_{2}

Or, (0.16)_{10} = (0.00101)_{2} (approx. value)

Where, (base 10) means the number is in decimal number system and (base 2) means the number is in binary number system.

Now, to get the binary of the decimal number 10.16 we have to merge the two binary results.
(10)_{10} = (1010)_{2}

(0.16)_{10} = (0.00101...)_{2}

So, (10.16)_{10} = (1010.00101...)_{2}

or, (10.16)_{10} = (1010.00101)_{2} (approx. value)

- Docker - Commands Docker
- Docker - How to build docker image and publish to DockerHub Docker
- Docker - Getting Started Docker
- Docker - run command Docker
- PostgreSQL - LEFT JOIN PostgreSQL
- PostgreSQL - INNER JOIN PostgreSQL
- PostgreSQL - ORDER BY and LIMIT PostgreSQL
- PostgreSQL - Alias PostgreSQL
- PostgreSQL - DELETE FROM Table PostgreSQL
- PostgreSQL - UPDATE Table PostgreSQL