Skip to the code
cf.
+10 XP

#667 Valid Texture Size

easy

In WebGL 1, a texture can only use mipmaps when its width and height are powers of two: 1, 2, 4, 8, .... An asset checker warns about other sizes.

Write isPowerOfTwoSize(n) that returns true if n is a power of two and false otherwise. Zero and negative numbers are never powers of two.

Example 1

Input: n = 256
Output: true

256 = 2^8, which is 100000000 in binary: a single 1 bit.

Example 2

Input: n = 300
Output: false

300 is 100101100 in binary, with more than one 1 bit.

Constraints

  • -2^31 <= n <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).