 
Patrick B. answered  03/10/19
Math and computer tutor/teacher
First we need to build the table that contains the powers of 2.
So how many binary bits does the number require? Let's say we are converting 32 bit whole
number and 32 bit decimal number.
So we need 32 bit array of bits for the whole number and 32 bit array of the decimal number.
2^32 = 4294967296 = bits[32]
2^31 = 2147483648 = bits[31]
2^30 = 1073741824= bits[30]
2^29 = 536870912=bits[29]
2^28 = 268435456 - bits[28]
....
2^15 = 32768 = bits[15]
....
2^8 = 256 = bits[8]
2^7 = 128 = bits[7]
2^6 = 64 = bits[6]
2^5 = 32 = bits[5]
2^4 = 16 = bits[4]
2^3 = 8 = bits[3]
2^2 = 4 = bits[2]
2^1 = 2 = bits[1]
2^0 = 1 = bits[0]
Then you do the same for the decimal number
2^0 = 1 = bits[0]
2^(-1) = 0.5 = bits[1] <--- 1/2
2^(-2) = 0.25 = bits[2] <--- 1/4
2^(-3) = 0.125 = bits[3] <--- 1/8
2^(-4) = 0.0625 = bits[4] <---- 1//16
etc
Next you separate the whole number from the decimal part.
Assign the whole number to variable X and decimal to variable Y
for each bit in the whole number, i=32 to 1
if X > bits(i) then
sets that bit to 1
subtracts X = X - bit(i)
else
sets that bit to zer0
end if
end for loop
So basically, if the remainder that's left is greater than the bit value, you set the bit to 1;
otherwise, you set it to zer0
for each bit in the decimal number, i=1 to 32
if Y > bits(i) then
sets that bit to 1
subtracts X = X - bit(i)
else
sets that bit to zer0
end if
end for loop
======================
Here is an example:
123.789
the highest power of 2 is 128 = 2^7. each bit before that shall be zero
So starting with bit 7, which will be zero.
bit # value bit set X - bit value
7 128 0 123
6 64 1 59
5 32 1 27
4 16 1 11
3 8 1 3
2 4 0 3
1 2 1 1
0 1 1 0
So the decimal portion is 01111011
the decimal is Y=0.789
bit # value bit set Y - bit value
1 0.5 1 0.289
2 0.25 1 0.039
3 0.125 0 0.039
4 0.0625 0 0.039
5 0.03125 1 0.00775
6 0.015625 0 0.00775
7 0.0078125 0 0.00775
8 0.00390625 1 0.0384375
9 0.00390625 0 0.0384375
10 0.001953125 1 0.036484375
etc
https://www.geeksforgeeks.org/convert-decimal-fraction-binary-number/
 
     
             
                     
                    