lowbit(x):返回x的最后一位一

x&-x=x&(-x+1)

例题 .二进制中1的个数

#include <iostream>
using namespace std;

int lowbit(int x)

{

return x&-x;

}

int main()

{
int n;
cin>>n;
while(n--)
{
int m;
cin>>m;
int res=0;
while(m) m-=lowbit(m),res++;
cout<<res<<endl;
}
return 0;
}

posted @ 2021-09-03 20:36  小白QIU  阅读(56)  评论(1)    收藏  举报