Given an integer n, return true if it is a power of two, otherwise return false. An integer n is a power of two if there exists an integer x such that n == 2ˣ.
n = 1truen = 16truen = 3falseA power of 2 like 8 (1000) has exactly one set bit. Subtracting 1 gives 7 (0111) — all bits below the set bit flip to 1, and the set bit itself becomes 0. The AND of 8 & 7 = 0. For any number that is not a power of 2, like 6 (110), subtracting 1 gives 5 (101). The AND 6 & 5 = 4 ≠ 0, so the check fails. The extra n > 0 guard handles 0 and negatives, since 0 & −1 would otherwise give 0 incorrectly.