I'm working on a Genetic Algorithmic program and now I'm trying to convert decimal number into binary array.
I've written this code to do that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
float D = GA_pop [0][0];
int k, h;
int bin [r];
k = (D -20) /c;
for (column = 0; column < r; column ++){
if ((k%2) != 0){
bin [column] = 1;
h = k/2;
}
elseif ((k%2) == 0){
bin [column] = 0;
h = k/2;
}
k=h;
}
That works but I'm trying to make a function. Here is the function:
It must be known at compile time. (¿is that a problem?)
http://cplusplus.com/reference/bitset/bitset/ std::bitset<16> bin(number); here we use a representation of 16 bits (the size must be known at compile time).
To access individual bits you could use operator[] by instance
1 2 3 4
for( int K=0; K<father.size()/2; ++K)
child[K] = father[K];
for( int K=mother.size()/2; K<mother.size(); ++K)
child[K] = mother[K];