Resource · Guide or article
Is a power of two
A classic interview task: decide whether a positive integer is a power of two, solved by repeated division and by a bitwise test.
Reviewed
Open the original: Is a power of two (opens the original site in a new tab)Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
Given a positive integer, write a function to find if it is a power of two or not. In naive solution we just keep dividing the number by two unless the number becomes 1 and every time we do so, we check that remainder after division is always 0. Otherwise, the number can't be a power of two. Powers of two in binary form always have just one bit set. The only exception is with a signed integer (e.g. an 8-bit signed integer with a value of -128 looks like: 10000000)
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- Naive repeated division by two with a remainder check
- The single-set-bit property, with signed -128 noted as an exception
- The number AND (number - 1) test traced for 8
How to use it
PrepKit study advice, not a claim made by the source.
Write both solutions, test 0, 1, a negative, 8 and 10, and explain why subtracting one flips the lowest set bit and those below it.
Scope and limitations
No complexity is stated and there is no code beyond bit diagrams.
Access and format
Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/math/is-power-of-two/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.