Kato_Yang wrote...
@Lightfoot8: Thanks a lot for the infos and corrections. The switch in GetBitRank() is not a problem but I'll search for the formula which could replace it(as a math practice).
Kato
Ok, I am confused now. ( Not hard to do, by the way.) you wrote.
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...
... 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), ...
...Thanks for any insight '>
If I understand you correctly, I guess I did not give enough insight.
First I have not tested your formula for GetHighestBit(ByValue), There is only one concern I have on that function, that being if the 31 bit is set ( any negtive number), I do not know if it will retrive the correct value., with the way the >> operator is bugged in NWScript.
the concern I have with the GetBitRank function is the number of comparisons that need to be done. We are looking at 32 comparisons if only the first bit is set. This will increase the chance of a "too many instructions" error if this function is ever used in a loop.
The function I posted will use less VM instructions to do the same thing.
I would also not be supprised if it could be used to get the get the highest bit value faster.
example.
int GetHighestBitByPosition(int nNum)
{
if (nNum==0) return -1; // Error case no bits set.
if (nNum <0 )return 31; // High bit set.
return FloatToInt(log(nNum*1.0)/log(2.0)); // return for bits 0 - 30
}
int GetHighestBitByValue(int nNum)
{
if(!nNum) return 0;
return 1 << GetHighestBitRankByPosition(int nNum)
}
Also if you feed the function a number with only a single bit set, it would by default be the highest bit set.
If that is not what you are asking, I am still confused