题目:求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
分析:使用移位统计连续出现1的次数,每次统计完与前面的连续次数作比较比前面的大就更新
int max_cur_bit1()
{
int n;
cin >> n;
int max = 0;
while (n)
{
int count = 0;
while (n & 1 == 1)
{
count++;
n = n >> 1;
}
//出来了,说明1不连续了
//比较和之前的max,大的话就更新max
max = count > max ? count : max;
n = n >> 1;
}
return max;
}