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

How are variables declared in c?

600


What are the 4 types of organizational structures?

626


What does typedef struct mean?

663


What extern c means?

546


Explain the properties of union. What is the size of a union variable

718






What is difference between arrays and pointers?

584


Why do we use static in c?

635


Program will then find the largest of three numbers using nested if-else statements. User is prompted to enter three numbers. Program will find the largest number and display it on the screen. All three numbers entered by the user are also displayed. If user enters 21, 33, and 5, the output should be as follows: You entered: 21, 33 and 5. The largest number is 33.

1015


How is pointer initialized in c?

588


What functions are in conio h?

662


What is infinite loop?

631


What is substring in c?

642


please send me the code for multiplying sparse matrix using c

1728


Is linux written in c?

602


Differentiate between the expression “++a” and “a++”?

706