 # Problem with Binary Array to Integer conversion

I have been having a very weird problem with a binary array (boolean) to integer conversion method i wrote a while ago. It worked well when converted from bool array to unsigned int but now that i changed int to long, its gone mad. I have tried both pow() function and bitshift operator and neither work correctly. Some help would be appreciated.

The function takes a 22 bool array and tries to convert it to a 4 bit long and an 18 bit long (which is why i cant use int anymore) based on the starting bit and the number of total bits. here is the code:

``````int i=0;
unsigned long buttoncode=0;
unsigned long commandcode=0;
boolean bit={0,0,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,1,1}; //3, 112387, 28096

void setup(){
Serial.begin(115200);
buttoncode=BtoI(0, 4);
commandcode=BtoI(4, 18);
Serial.print(commandcode);
Serial.print("\t");
Serial.println(buttoncode);
}

void loop(){}

unsigned long BtoI(int start, int numofbits){    //binary array to integer conversion
unsigned long integer=0;
i=start;
int n=0;
while(i<numofbits+start){
integer+=pow(bit[i]*2, (numofbits-n-1)); //same as pow()
Serial.print(integer);
Serial.print("\t");
Serial.println(numofbits-n-1);
i++;
n++;
}
Serial.println();
return integer;
}
``````

this gives this result:

``````0      3

0      2

1      1

2      0

0      17

65535      16

98302      15

98302      14

106493      13

110588      12

110588      11

111612      10

112124      9

112380      8

112380      7

112380      6

112380      5

112380      4

112380      3

112380      2

112382      1

112383      0

112383      2
``````

anyone have any ideas? im pretty confused

Were you expecting the results below? : My guess is that the storage class for some of your variables are too small. And the array of booleans, try to make array of integers. Or is there something specific you are trying to do with the bit array?

0 3 0 2 2 1 3 0

0 17 0 16 32768 15 49152 14 49152 13 53248 12 55296 11 55296 10 55808 9 56064 8 56064 7 56128 6 56160 5 56176 4 56176 3 56176 2 56176 1 56177 0

i created my own power2 function and that works perfectly. it shouldnt be the holding class thats the problem becayse unsigned long should be overkill for 22 bits. my power2 function gives the correct output which is : 0 3 0 2 2 1 3 0

0 17 65536 16 98304 15 98304 14 106496 13 110592 12 110592 11 111616 10 112128 9 112384 8 112384 7 112384 6 112384 5 112384 4 112384 3 112384 2 112386 1 112387 0 which is what it should be

if you compare them, the pow()'s are off by one in each the 2^16, 2^14, 2^13, and 2^12.

Read my discussion of the limitations and purpose of the pow() function here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234547299/1#1

well that pretty much answers it. thanks a lot for your help. now i know firsthand the dangers of floating point math

Maybe something like this would be a bit more economical:

``````unsigned long BtoI(int start, int numofbits){    //binary array to integer conversion
unsigned long integer=0;
for (i = numofbits+start-1; i >= start; i--) {
``````if (bit[i]) integer |= mask;