64,639
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
void main()
{
int i=1023;
int n=0;
while(i)
{
i<<=1;
if(i<0)
n++;
}
cout<<n<<endl;
}
#include<iostream>
using namespace std;
void main()
{
int i=1023;
int n=0;
while(i)
{
i &= i-1;
n++;
}
cout<<n<<endl;
}
#include<iostream>
using namespace std;
我也扔一砖头……
int main()
{
unsigned int i=1023;
int n=0;
while(i)
{
n += i&1;
i>>=1;
}
cout<<n<<endl;
return 0;
}
char table16[16] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4} ;
char table256[256];
void SetData();
unsigned int bitcount32_1(unsigned int x);
unsigned int bitcount32_2(unsigned int x);
unsigned int bitcount32_3(unsigned int x);
unsigned int bitcount32_4(unsigned int x);
unsigned int bitcount32_1(unsigned int x)
{
x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
x = (x & 0x0f0f0f0fUL) + ((x >> 4) & 0x0f0f0f0fUL);
x = (x & 0x00ff00ffUL) + ((x >> 8) & 0x00ff00ffUL);
x = (x & 0x0000ffffUL) + ((x >> 16) & 0x0000ffffUL);
return x;
}
unsigned int bitcount32_2(unsigned int x)
{
x = x - ((x >> 1) & 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
x = (x + (x >> 4)) & 0x0f0f0f0fUL;
x += x >> 8;
x += x >> 16;
return x & 0x3f;
}
unsigned int bitcount32_3(unsigned int x)
{
return table16[x&0xf]+table16[(x>>4)&0xf]+table16[(x>>8)&0xf]+table16[(x>>12)&0xf] +
table16[(x>>16)&0xf]+table16[(x>>20)&0xf]+table16[(x>>24)&0xf]+table16[(x>>28)&0xf];
}
unsigned int bitcount32_4(unsigned int x)
{
return table256[x&0xff] + table256[(x>>8)&0xff]+
table256[(x>>16)&0xff] + table256[(x>>24)&0xff];
}
void SetData()
{
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
table256[i*16+j] = table16[i] + table16[j];
}
#include <iostream>
using namespace std;
int main()
{
int i = 1023;
int cnt = 0;
for (; i; i &= i-1, ++cnt);
cout << cnt << endl;
return 0;
}