Leetcode Problem 231. Power of Two

231. Power of Two

Leetcode Solutions

Bitwise AND of Number and Number Minus One

  1. Check if n is less than or equal to 0. If it is, return false because negative numbers and zero cannot be powers of two.
  2. Perform a bitwise AND operation between n and n - 1.
  3. If the result of the AND operation is 0, return true, indicating that n is a power of two.
  4. If the result is not 0, return false, indicating that n is not a power of two.
UML Thumbnail

Bitwise AND of Number and Negative Number

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...