I have to write a program that counts the number of bits set in an integer. For example, the number 5 (decimal), which is 0000000000000101 (binary), has two bits set. I know how to count the number of bits in a char, but I don't know how to count the extra 8 bits in an integer. How would i navigate to the other byte and count the eight bits?
Here is how i'd count the number of bits in a char...
Thanks for any help with this...Code:#include <stdio.h> void main() { char ch = 5; /*assign it a random number*/ int counter = 0; int i; for (i = 0x80; i > 0; i = (i >> 1)) { if ((ch & i) != 0) ++counter; } }