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 what is wrong with this statement? Myname = ?robin?;

0 Answers  


What are the types of unary operators?

0 Answers  


1.what are local and global variables? 2.what is the scope of static variables? 3.what is the difference between static and global variables? 4.what are volatile variables? 5.what is the use of 'auto' keyword? 6.how do we make a global variable accessible across files? Explain the extern keyword? 7.what is a function prototype? 8.what does keyword 'extern' mean in a function declaration?

2 Answers   nvidia,


Write a program that can show the multiplication table.

0 Answers   Student,


What is a floating point in c?

0 Answers  






What is the argument of a function in c?

0 Answers  


1. main() { printf("%d",printf("HelloSoft")); } Output?

3 Answers   HCL,


Why cd or dvd are round why not square.

1 Answers  


can we declare a function inside the structure? ex: struct book { int pages; float price; int library(int,float); }b; is the above declaration correct? as it has function declaration?

2 Answers  


write an algorithm which can find the largest number among the given list using binary search ............... this was asked in the interview

2 Answers   Satyam, UNIS, Wipro,


#include<stdio.h> main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } Tell me the output?

6 Answers   Ramco,


Write a program to exchange two variaables without temp

9 Answers   Geometric Software,


Categories