Binary AND Operation

n

8
Input number

n - 1

7
Subtract one

Current Bit

-
Position

Result

-
n & (n-1)
Click "Step" or "Play" to visualize the AND operation bit by bit
Bit = 1
Bit = 0
Active
Result
Python Code - Power of Two
def isPowerOfTwo(n):
    if n == 0:
        return False
    return n & (n - 1) == 0
 
# Power of 2: only one bit set
# 8 = 1000, 7 = 0111
# 1000 & 0111 = 0000 (zero!)