#include<iostream>
#include<string>
using namespace std;
int max1(int n){
//unsigned int flag = 1;
int count = 0;
int max = 0;
if (n<0 || n>255)
return 0;
while (n!=0){
while ((n &1) == 1 && n != 0){ //位运算需要用括号连起来
count++;
n >>=1;
}
if (max < count)
max = count;
while ((n & 1) != 1 && n != 0)
n >>= 1;
count = 0;
}
return max;
}
void main(){
int n;
cin >> n;
cout << max1(n) << endl;
system("pause");
}
最大连续的bit数(二进制1的个数)
最新推荐文章于 2024-08-01 18:59:15 发布