I\'ve bool arrays of sizes : 32, 48, 64 (each boolean represents a bit). how can I convert them to a number with a good performance( int, unsigned int, double48, double64)?
O(n):
int bitArrayToInt32(bool arr[], int count)
{
int ret = 0;
int tmp;
for (int i = 0; i < count; i++) {
tmp = arr[i];
ret |= tmp << (count - i - 1);
}
return ret;
}
int main()
{
bool ar[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1};
int num = bitArrayToInt32(ar,32);
printf("number = %d\n", num);
}