This paste expires on 2027-11-05 17:21:01. Repaste, or download this paste. . Pasted through v1-api.

# Binary Number with Alternating Bits (LeetCode 693)
Check whether adjacent bits always differ.
```python
def hasAlternatingBits(n):
    x = n ^ (n >> 1)
    return (x & (x + 1)) == 0
```
XOR with the right-shift; then one contiguous block of 1-bits means alternating.
Practice with [Linkjob AI](https://www.linkjob.ai/interview-copilot/).
(Affiliation: I work on Linkjob AI.)
Filename: note.md. Size: 382b. View raw, , hex, or download this file.