I have an issue with my Arduino. I am trying to convert a boolean array into an int with this piece of code:
int boolean_to_decimal(bool bol[]) {
int somme=0;
for (int i = 0; i < 6; i++){
somme += bol[i] * pow(2, 5-i);
}
return somme;
}
Nothing really impressive but here are my results:
010101 == 20 (instead of 21)
100101 == 36 (instead of 37)
101001 == 40 (instead of 41)
011001 == 23 (instead of 25)
etc
Thank you for your time, David
Using floating-point function pow()
for integers seems bad because it may contain errors. Try using bit-shifting instead.
int boolean_to_decimal(bool bol[]){
int somme=0;
for (int i = 0; i<6; i++){
somme += bol[i]*(1 << (5-i));
}
return somme;
}