Algorithms and Data Structures · Senior · Question 48 of 75
What is the complexity of checking whether a number is a power of two with n > 0 && (n & (n - 1)) === 0?
- ✓Correct answer: O(1)
- 2O(log n)
- 3O(n)
Explanation
It's a single bitwise operation on a fixed-width integer. In JavaScript bitwise operators work on 32-bit integers, so for values above 2³¹ you need another approach, such as Number.isInteger(Math.log2(n)) or BigInt.