1.每次移位获取最后一位的二进制
 
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	int n;
	while (cin >> n) {
		int count = 0, maxCount = 0;
		while (n) {
			//获取当前位的二进制值
			if (n & 1) {
				//如果1的值连续,计数累加,并且跟新最大计数
				++count;
				maxCount = max(count, maxCount);
			}
			else {
				count = 0;
			}
			n = n >> 1;
		}
		cout << maxCount << endl;
	}
	return 0;
}
 
2.网上看到的,很精简
 
#include<iostream>
using namespace std;
int main(){  
	int byte; 
	while(cin>>byte) 
	{       
		int k=0;  
		for( k=0;byte!=0;k++)   
		{        
			byte=byte&(byte<<1);  
                   //移位前与移位后与为1,则说明连续,重新赋值消除影响 
		}       
		cout<<k<<endl;   
	}    return 0;
}