what is the purpose of the code, and is there any problem
with it.
bool f( uint n )
{ return (n & (n-1)) == 0; }



what is the purpose of the code, and is there any problem with it. bool f( uint n ) { return ..

Answer / c.p.senthil

function returns if the number is a power of 2. if number value is 1, 2, 4, 8, 16 ... then TRUE is returned.

f(1) => 1 & 0 => 0001 & 0000 => 0
f(2) => 2 & 1 => 0010 & 0001 => 0
f(3) => 3 & 2 => 0011 & 0010 => 1 (non zero) => return true
f(4) => 4 & 3 => 0100 & 0011 => 0
f(5) => 5 & 4 => 0101 & 0100 => 4 (non zero) => return true
f(6) => 6 & 5 => 0110 & 0101 => 4 (non zero) => return true
f(7) => 7 & 6 => 0111 & 0110 => 6 (non zero) => return true
f(8) => 8 & 7 => 1000 & 0111 => 0

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Interview Questions

Explain the term printf() and scanf() used in c language?

0 Answers  


What is an lvalue?

0 Answers  


write aprogram for There is a mobile keypad with numbers 0-9 and alphabets on it. take input of 7 keys and then form a word from the alphabets present on those keys.

1 Answers   iGate, Shashi, Source Bits, Subex,


Why is c called a structured programming language?

0 Answers  


Explain what is the difference between functions abs() and fabs()?

0 Answers  






What is a buffer in c?

0 Answers  


# define prod(a,b)=a*b main() { int x=2; int y=3; printf("%d",prod(x+2,y-10)); } the output of the program is a.8 b.6 c.7 d.none

7 Answers   Microsoft, TCS,


control 50 devices which has 2 states on and off.using bitwise operator.plz answer it its urgent

1 Answers  


code for concatination of 2 strings with out using library functions?

3 Answers  


What is unary operator?

0 Answers  


Differentiate between null and void pointers.

0 Answers   TCS,


main() { inta=10,b=20; a>=5?b=100:b=200; printf("%d ",b); }

0 Answers   Wilco,


Categories