# Conversion between boolean and int went wrong

I have a small problem with conversion from bool array to int. It is working fine until it comes to the 12th cycle of while. I don't know why but this is really weird.

I try to paste here a code where I am doing this conversion.

``````int* BoolToInt(bool* set) {
int* ret = (int*)malloc(3*sizeof(int));
int sum0 = 0;
int sum1 = 0;
int sum2 = 0;
//bool test[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
for(int m=0; m<24; m++) {
if(m<8) {
sum0 += (int)set[m] * (pow(2,m));
}
if(m>7 && m<16) {
sum1 += (int)set[m] * (pow(2,(m-8)));
}
if(m>15 && m<24) {
sum2 += (int)set[m] * (pow(2,(m-16)));
}
}
ret = sum0;
ret = sum1;
ret = sum2;
Serial.print("\n ret = ");
Serial.print(ret);
Serial.print("\n ret = ");
Serial.print(ret);
Serial.print("\n ret = ");
Serial.print(ret);
return ret;
}
``````

And this is where I am using this function:

``````void Fitness(bool** set) {
int maX = 0;
int index = 0;
int* temp;
for(int i=0; i<10; i++) {
int* phenotype = BoolToInt(set[i]);
_fitness[i] = 1000/((phenotype - _colours)*(phenotype - _colours) + (phenotype - _colours)*(phenotype - _colours) + (phenotype - _colours)*(phenotype - _colours) + 1);
Serial.print("\n phenotype = ");
Serial.print(phenotype);
Serial.print("\n phenotype = ");
Serial.print(phenotype);
Serial.print("\n phenotype = ");
Serial.print(phenotype);
free(phenotype);
}
for(int i=0;i<10;i++) {
temp = BoolToInt(set[i]);
if(_fitness[i]>maX) {
maX = _fitness[i];
index = i;
}
}
setColorOnSecond(temp[index]);
free(temp);
}
``````

As seen in the Fitness function, I have some debugging prints to see if everything is fine. My main is in the while cycle. Everything is fine and "ret" from BoolToInt function is the same as "phenotype" in Fitness, which is a good thing and this is what I want. But then the 12th cycle comes and phenotype gives me 1200 as int. And of course everything in ret[] is fine(0-255). Am I doing something wrong?

NOTE: bool* set in BoolToInt is 24 bool array (yes 3 numbers as you can see).

NOTE 2: every previous cycle before 12th cycle is good and the result are equal.

• Are you trying to encode a bool array into an integer? What `while`cycle are you talking about? – user31481 Nov 27 '17 at 21:13
• Your code doesn't compile. Some variables are missing. – user31481 Nov 27 '17 at 21:16
• yes of course it won't compile.. this is only a part of evolutionary algoritm. – Apuna12 Nov 27 '17 at 21:29
• while is used in main....imagine it as a while and inside of that is this Fitness function – Apuna12 Nov 27 '17 at 21:32
• Imagine doesn't work. Post a complete, minimal, working sketch. We can't speculate on unseen code. – user31481 Nov 27 '17 at 21:55

Your problem is using pow. It works in floats. It may come as some surprise, but pow(2,3) gives 7 when you put it into an int.

Don't waste time on pow for powers of 2. Just use a bitshift. It's way smaller and faster.

• Where did you see that pow(2, 3) gives 7? My Arduino tells me `(int) pow(2, 3) == 8`. – Edgar Bonet Nov 27 '17 at 22:04
• Oh, wait, you're right. It gives 8 if `3` is a constant, but 7 if it's a variable. – Edgar Bonet Nov 27 '17 at 22:17
• I've seen enough threads on Arduino.cc where someone is shocked that Arduino gets powers of 2 wrong to know that one pretty well. I'm guessing that when it is done with constants the calculation is being optimized away and the compiler is smart enough to put 8 in there. – Delta_G Nov 27 '17 at 22:21
• this helped me :)... now it gives me good answers :).. thanks – Apuna12 Nov 28 '17 at 20:58

Let me the first one to say that 1. Boolean to int conversion shouldn't be this complicated and 2. Much simpler ways to do what you are trying to do.

Like this.