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
How are variables declared in c?
What are the 4 types of organizational structures?
What does typedef struct mean?
What extern c means?
Explain the properties of union. What is the size of a union variable
What is difference between arrays and pointers?
Why do we use static in c?
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.
How is pointer initialized in c?
What functions are in conio h?
What is infinite loop?
What is substring in c?
please send me the code for multiplying sparse matrix using c
Is linux written in c?
Differentiate between the expression “++a” and “a++”?