Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 646 Bytes

File metadata and controls

22 lines (16 loc) · 646 Bytes

Description:

Complete the function power_of_two/powerOfTwo (or equivalent, depending on your language) that determines if a given non-negative integer is a power of two. From the corresponding Wikipedia entry:

'a power of two is a number of the form 2n where n is an integer, i.e. the result of exponentiation with number two as the base and integer n as the exponent.'

You may assume the input is always valid.

Examples (input --> output):

isPowerOfTwo(1024) // -> true
isPowerOfTwo(4096) // -> true
isPowerOfTwo(333)  // -> false

Solution:

function isPowerOfTwo(n){
  return Math.log2(n) % 1 === 0;
}