I have this function here to count 1
bits in a byte. However, when I try putting the char
value of 200, it breaks. However, if I changed the char
to unsigned char
, it works. I am curious as to why.
int bit_counter (char b){
char count = 0;
while (b != 0){
if (b & 0x01){
count ++;
}
b = b >> 1;
}
return count;
}
I have solved this issue. I masked all the bits, but the most significant bit.
int bit_counter (char b){
char count = 0;
while (b != 0){
if (b & 0x01){
count ++;
}
b = b >> 1;
b = (b & 0x7F);
}
return count;
}