#include<bits/stdc++.h>
#include<vector>
using namespace std;
int f1(int x)
{
    int c=0;
    while(x>0)
    {
        if((x&1)==1)//判断最后一位是否为1
            c++;
        x>>=1;//右移一位
    }
    return c;
}
int f2(int x)
{
    int c=0;
    while(x)
    {
        c++;
        x=x&(x-1);//从低位开始,把遇到的第一个1置为0
    }
    return c;
}
int main()
{
    cout<<f1(7)<<endl;
    cout<<f2(7)<<endl;
    return 0;
}

相关文章:

  • 2021-11-08
  • 2022-02-23
  • 2022-12-23
  • 2021-11-20
  • 2021-10-19
  • 2022-02-17
  • 2022-12-23
猜你喜欢
  • 2021-12-06
  • 2021-11-05
  • 2021-12-03
  • 2022-02-01
  • 2021-12-16
  • 2022-12-23
相关资源
相似解决方案