Hi All,
I want to check how many bits are set in an integer .
For example,
int a = 10;
The binary form of 10 is 1010.
There are two bits set to 1 in the number 10.
Like that i have check how many bits are set to 1 in an integer.
Can anyone help me in this....
I need to find it without using any loops...
Are there any other constraints imposed on this homework problem?
Another approach is divide-and-conquer: recognize that you can break your input value into smaller chunks (bytes, nibbles, or even bits) and that the sum of the number of bits set in all the chunks is the same as the number of bits set in the input value. For a small enough chunk it becomes efficient to use a lookup table. However, you can't break the input value into chunks unless you know how many chunks fit in the input type. Take a look at <limits.h> to see if you can think of a reasonably portable way to do this.
Cheers,
donbock