Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What are local static variables?

1150


What are the advantages of the functions?

1160


What is operator precedence?

1204


When is a void pointer used?

1240


What is 1d array in c?

1091


What is time complexity c?

994


Explain what is the use of a semicolon (;) at the end of every program statement?

1226


Linked lists -- can you tell me how to check whether a linked list is circular?

1083


Why main is not a keyword in c?

1254


Differentiate between the = symbol and == symbol?

1294


How are 16- and 32-bit numbers stored?

1289


Where are the auto variables stored?

1147


what is recursion in C

1064


What are the properties of union in c?

1055


What functions are used for dynamic memory allocation in c language?

1151