Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int bitcount(int n) {
- unsigned int c1 = 0;
- for (; n > 0; n >>= 1)
- if (n & 1) {
- c1++;
- }
- return c1;
- }
- int main() {
- int a = -2;
- printf("%u n", bitcount(a));
- return 0;
- }
- int bits(unsigned int x)
- {
- x = x - ((x >> 1) & 0x55555555);
- x = (x & 0x33333333) + ((x>>2)&0x33333333);
- x = (x + (x >> 4)) & 0x0F0F0F0F;
- x += x >> 8;
- x += x >> 16;
- return x&0x3F;
- }
- int main()
- {
- for(int i = -5; i <= 5; ++i)
- printf("%3d %5dn",i,bits(i));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement