FoundationMultiple choice
For a positive integer , which test is true exactly when is a power of two?
- A
x % 2 == 0 - B
(x & (x - 1)) == 0 - C
(x | (x - 1)) == 0 - D
(x ^ (x >> 1)) == 1
The worked solution is in Premium
The answer, the full working and the one idea to take away – for this and all 1,322 questions in the bank. Answer it in practice and your working is marked, with a known mistake named when you make one.
Learn the method
Reported in interviews at
More data structures and algorithms questions
- How many set bits does the integer 90 have?Foundation
- Finding the unpaired ids, part 1 of 3Foundation
- How many iterations does the loop while (x) { x &= x - 1; n++; } make on x = 255?Applied
- What is 1oplus2oplus3oplus⋯oplus10, where oplus is bitwise XOR?Applied
- Computing x¹³ by binary exponentiation (repeated squaring), how many…Applied
- Finding the unpaired ids, part 2 of 3Applied