private int GetHighestBitOnePos(int data, int dataBitsCount)
{
int highestBitOnePosition = 0;
// get half bits count of maxBits, if there is remainder, add the remainder
int maskBitsCount = dataBitsCount / 2 + dataBitsCount % 2;
while (true)
{
// the mask form is like 11110000
int mask = ((1 << maskBitsCount) - 1) << (dataBitsCount - maskBitsCount);
if ((mask & data) != 0)
{
// if high-bits part of data is not zero
highestBitOnePosition += (dataBitsCount - maskBitsCount);
data = data >> (dataBitsCount - maskBitsCount);
}
if (maskBitsCount == 1)
{
// if the maskBits's length become 1, then we can get the highest bit position
// of data. it counts from 1.
return (highestBitOnePosition + 1);
}
dataBitsCount = maskBitsCount;
// reduce maskbits' length by half, if there is remainder, add the remainder.
maskBitsCount = dataBitsCount / 2 + dataBitsCount % 2;
}
}