Describe a function to check if an integer is a power of 2.
Respostas da entrevista
Sigiloso
27 de jul. de 2011
Write the number in binary and count the number of ones in that.If the number os ones is only 1 then it the number is indeed a power of 2
9
Sigiloso
30 de jan. de 2010
For an integer n:
If n is less than 1, return false.
If the bitwise & of n and n-1 is 0, return true.
Otherwise, return false.
12
Sigiloso
23 de jun. de 2015
I think the main idea is to use recursion function, for the integer which is larger than 0, if it is 1 return true, else return function(n-1)
1
Sigiloso
2 de mar. de 2019
function int power2(int i);
if (i%2==1) return 0;
else if (i/2==1) return 1;
else return power2(i/2);
endfunction
Sigiloso
18 de set. de 2015
See if the sum of all bits is 1. If that's the case then the number is a power of 2.
1
Sigiloso
27 de jul. de 2011
first check if no is 1 then return false else write the number in binary and then check number of ones in that.if only one 1 is there then its a power of 2