Sorry for the late reply. I was away.
At any rate you have posted a bit of useful knowledge.
Knowledge that many ignore, and that many others once knew but have forgot.
Allow me to contribute to your thread.
Another nice trick:
int GetIsPow2 (int nValue)
{
return (nValue & -nValue == nValue);
}
This returns false if the number is not a power of 2.
It will return true if the number is a power of 2.
Be sure to feed to the function only a positive integer (nwscript has no meaning of "unsigned", so the function may not be secured in that sense -- assuming you do not want to introduce a call to abs() ).
The trick is simple. An integer number is a power of 2 if 1 and only 1 bit of it is set.
Two examples:
--------------------
With nValue=8
Bit pattern of +8 is: 00001000
Bit pattern of -8 is: 11111110
AND-ing them produces 00001000.
The comparison between 00001000 and 00001000 produces TRUE.
In fact 8 is a power of 2.
--------------------
With nValue=250
Bit pattern of +250 is: 11111010
Bit pattern of -250 is: 00000110
AND-ing them produces 00000010.
The comparison between 11111010 and 00000010 produces FALSE.
In fact 250 is not a power of 2.
-fox
Modifié par the.gray.fox, 11 janvier 2011 - 02:51 .