Function to find the given number is a power of 2 or not?
Answer Posted / hassan noureddine
To be a power of 2 number,is to have a single 1 bit, and the
rest bits are zeros, lik2 1, 2, 4 , 8, 16, 32, 64, 128, ...
the bitsize of the number is sizeof(number) * 8
isPowerOf2() returns 1 if successful, or 0 (false) otherwise
int isPowerOf2 (number)
{
int foundOnes = 0;
int bitsize = sizeof(number) * 8;
for (i = 0; i < bitsize; i++)
{
if (number & 1)
{
if(++foundOnes > 1)
return false;
/* shift the number to the right */
number >> 1;
}
}
return foundOnes;
}
| Is This Answer Correct ? | 20 Yes | 12 No |
Post New Answer View All Answers
In a switch statement, explain what will happen if a break statement is omitted?
Can I initialize unions?
Explain what header files do I need in order to define the standard library functions I use?
State the difference between x3 and x[3].
Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10
what is uses of .net
What is struct node in c?
Apart from dennis ritchie who the other person who contributed in design of c language.
if (i = 0)printf ("True"); elseprintf("False"); Under what conditions will the above print out the string "True" a) Never b) Always c) When the value of i is 0 d) all of the above
What is include directive in c?
Are negative numbers true in c?
WHICH TYPE OF JOBS WE GET BY WRITING GROUPS .WHEN THE EXAMS CONDUCTED IS THIS EXAMS ARE CONDUCTED EVERY YEAR OR NOT.PLS TELL ME THE ANSWER
What is the use of getchar functions?
What happens if you free a pointer twice?
C program to find all possible outcomes of a dice?