Skip to content

Instantly share code, notes, and snippets.

@sikanrong
Created July 30, 2018 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sikanrong/4cdf64fa087c825a9ef4b04dab070c5c to your computer and use it in GitHub Desktop.
Save sikanrong/4cdf64fa087c825a9ef4b04dab070c5c to your computer and use it in GitHub Desktop.
//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