Created
July 30, 2018 15:31
-
-
Save sikanrong/4cdf64fa087c825a9ef4b04dab070c5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Bitwise programming interview question | |
var isPowerOfFour = function(x){ | |
var bitIdx = 0; | |
var currentIdx = 0; | |
var bitCount = 0; | |
while(x){ | |
if(x & 1){ | |
bitCount++; | |
bitIdx = currentIdx; | |
} | |
currentIdx++; | |
x >>= 1; | |
} | |
return bitCount == 1 && (bitIdx % 2) == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment