Power of Two

easy · bit-manipulation, math

Power of Two

Given an integer n, return true if it is a power of two. Otherwise return false.

A power of two has exactly one bit set in its binary representation.

Function signature

func IsPowerOfTwo(n int) bool

Example

n = 16
output = true

Constraints

  • -1_000_000_000 <= n <= 1_000_000_000

Notes

  • 1 is a power of two.
  • 0 and negative numbers are not powers of two.
Run tests to see results
No issues detected
    Join Discord