Difference between revisions of "Number of bits set"

From Teknologisk videncenter
Jump to: navigation, search
m (Created page with "=C programming= The following function returns number of bits set i <source lang=c> int NumberOfSetBits32(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) +...")
 
m
 
Line 9: Line 9:
 
}
 
}
 
</source>
 
</source>
[[Category:C]
+
[[Category:C]]

Latest revision as of 07:00, 24 February 2012

C programming

The following function returns number of bits set i

int NumberOfSetBits32(int i)
{
    i = i - ((i >> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}