How to convert float number like -2.25 to binary? I need creating a population for a genetic algorithm with domain from -20.5 to 20.5 . Each chromosomes should be represinted with 0s and 1s only
1 = 01
0 = 00
4 = 100
and so on
I know how to convert the "2" but not the 0.25 nor the number with negative sign
this is my method and I'm stuck there. I could not know how to enhance it to give me the correct result when converting float, negative int or negative float number
public class MinimizeRastriginFunction {
public static String binary="";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//float x=(float) 2.5;
int x =5 ;
System.out.println("Before :" + x);
decToBin(x);
System.out.println("After :" + binary);
}
private static Object decToBin(int dec) {
// TODO Auto-generated method stub
int num;
if (dec <=1) {
binary +=dec;
return null; // KICK OUT OF THE RECURSION
}
num= dec %2;
decToBin(dec >>1);
binary += num;
return null;
}