Tuesday, April 27, 2010

Program to Find lsb and msb - C++

What is Least significant bit ( lsb ) ?

In computing, the least significant bit (lsb) is the bit position in a binary integer giving the units value, that is, determining whether the number is even or odd. The lsb is sometimes referred to as the right-most bit, due to the convention in positional notation of writing less significant digits further to the right.

template<class T>

//Least significant bit

unsigned short GetLSB(T tValue)

{

      return (tValue & 1);

}

What is Most significant bit ( msb ) ?

In computing, the most significant bit (msb) is the bit position in a binary number having the greatest value. The msb is sometimes referred to as the left-most bit on big-endian architectures, due to the convention in positional notation of writing more significant digits further to the left.
The msb can also correspond to the sign of a signed binary number in one or two's complement notation. "1" meaning negative and "0" meaning positive.
MSB, in all capitals, can also stand for "most significant byte". The meaning is parallel to the above: it is the byte (or octet) in that position of a multi-byte number which has the greatest potential value.
//Find Most significant bit
template<class T>
unsigned short GetMSB(T tValue)
{
      return ( tValue >> ( (sizeof(T) * 8) - 1) );
}

 

No comments: