Skip to content

Commit 352cb5e

Browse files
committed
Update binary-number-with-alternating-bits.py
1 parent 216abc0 commit 352cb5e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

binary-number-with-alternating-bits.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ def hasAlternatingBits(self, n):
44
:type n: int
55
:rtype: bool
66
"""
7-
bits = bin(n)[2:]
8-
return all(bits[i] != bits[i + 1] for i in range(len(bits) - 1))
7+
a = n ^ (n >> 1)
8+
return a & (a + 1) == 0

0 commit comments

Comments
 (0)