题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
n = int(input()) count = 0 max_count = 0 while n > 0: bit = n & 1 if bit == 1: count += 1 max_count = max(max_count, count) else: count = 0 n >>= 1 print(max_count)
用&将二进制n的最小位与1做与运算,记录连续1的个数,并输出最多个数
位右移n,直至为n=0跳出循环