In the process of completing an include file dedicated to bit functions, I'm not too sure about the right formula for retrieving the rank of a bit. I have seen on the web that the first 2 bits + the highest bit must be retrieved, but the formula is somewhat strangely explained(for a math noob like me I guess) in the docs I have found so far.
To get the highest bit(or rather bitwise value), I have found:
int GetHighestBit(int nBitArray)
{
nBitArray |= (nBitArray >> 1);
nBitArray |= (nBitArray >> 2);
nBitArray |= (nBitArray >> 4);
nBitArray |= (nBitArray >> 8);
nBitArray |= (nBitArray >> 16);
return nBitArray - (nBitArray >> 1);
}
It works well for 32 bit numbers, although it might possibly be simplified. So now, to retrieve the rank of a bit, I'm using this(shortened for the purposes of this post), until I get or correctly assess the right formula:
int GetBitRank(int nBit)
{
switch(nBit)
{
case 32: return 6;
case 16: return 5;
case 8: return 4;
case 4: return 3;
case 2: return 2;
case 1: return 1;
}
return 0;
}
Thanks for any insight
Kato
Modifié par Kato_Yang, 12 août 2012 - 04:52 .





Retour en haut






