1111: MSB=4, LSB=4
1101100: MSB=7, LSB=3
To find MSB: Right shift the operator till the number become zero. The number of time that you have right shifted is the MSB
To find LSB: Right shift the number(take out the last number 0 or 1) and then left shift( put a 0 back)
Now if the number before the operation is equal 0 after the operation then
count++;
rightshift the number
goto start
int msb(int *num)
{
if(*num >> 1 == 0)
{
return msbCount;
}
else
{
*num >>= 1;
msbCount++;
return msb(num);
}
}
int lsb(int *num)
{
if((*num>>1)<<1 != *num)
return lsbCount;
else
{
*num >>= 1;
lsbCount++;
return lsb(num);
}
}

Leave a Reply
You must be logged in to post a comment.