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
What are local static variables?
What are the advantages of the functions?
What is operator precedence?
When is a void pointer used?
What is 1d array in c?
What is time complexity c?
Explain what is the use of a semicolon (;) at the end of every program statement?
Linked lists -- can you tell me how to check whether a linked list is circular?
Why main is not a keyword in c?
Differentiate between the = symbol and == symbol?
How are 16- and 32-bit numbers stored?
Where are the auto variables stored?
what is recursion in C
What are the properties of union in c?
What functions are used for dynamic memory allocation in c language?