I need C code to return the number of 1\'s in an unsigned char in C. I need an explanation as to why it works if it\'s not obvious. I\'ve found a lot of code for a 32-bit nu
As already answered, the standard ways of counting bits also work on unsigned chars.
Example:
unsigned char value = 91; int bitCount = 0; while(value > 0) { if ( value & 1 == 1 ) bitCount++; value >>= 1; }