RSS

How will you find the MSB and LSB of a number

Fri, Mar 5, 2010

Algorithm

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);
         }
}

Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Other Posts:

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.