There is always a multiple ways to get a solutions for given problem and I am not going to argue this is the best.
int count_number_of_bit_set(unsigned value) { unsigned int shift[] = {1, 2, 4, 8, 16}; unsigned int magic_number[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; unsigned char i = 0; for (; i <= 4; i++) { value = (value & magic_number[i]) + ((value >> shift[i]) & magic_number[i]); } return value; }Let understand the logic inside the code,
If you like my blog, Please comment your views and feedback.
No comments:
Post a Comment