You need to right shift the number untill the number becomes zero and count the number of times right shifted, then you subtract it from the size of an interger (32 bit on a 32 bit machine), you will get the answer. Here is the code for your reference.
int fbs(unsigned int num)
{
int pos = -1;
while(num!=0)
{
num <<= 1;
pos++;
}
return 32 - pos;
}
No comments:
Post a Comment