40 lines
583 B
C
40 lines
583 B
C
#define S2
|
|
|
|
int hammingWeight(int n)
|
|
{
|
|
#ifdef S1
|
|
int weight = 0;
|
|
for (int i = 0; i < 32; i++)
|
|
if (n & (1u << i))
|
|
weight++;
|
|
|
|
return weight;
|
|
#endif
|
|
|
|
#ifdef S1_O
|
|
int weight = 0;
|
|
while (n)
|
|
{
|
|
n &= n - 1;
|
|
weight++;
|
|
}
|
|
|
|
return weight++;
|
|
#endif
|
|
|
|
#ifdef S2
|
|
unsigned i = (unsigned)n;
|
|
i = i - ((i >> 1) & 0x55555555);
|
|
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
|
|
i = (i + (i >> 4)) & 0x0f0f0f0f;
|
|
i = i + (i >> 8);
|
|
i = i + (i >> 16);
|
|
return (int)(i & 0x3f);
|
|
#endif
|
|
}
|
|
|
|
int main()
|
|
{
|
|
return 0;
|
|
}
|