>>Hi all,
>>
>>Problem is I need to do do something as a multipple of the bits set on an integer, the integer is a bit array to store statii.
>>
>>I need to figure out how many bits are set on an integer.
>>
>>I know I can loop / iterate through the number and bittest / divide by 2 etc.
>>
>>for now there are not to much so I can do with an ICASE, but I like it generic :)
>>
>>Any ideas for a fast solution? Something mathematical?
>>
>>Lutz
>
>If it is a positive integer, the number of bits is related to the base-2 logarithm. You may need to experiment a bit; but I think the number of binary digits is something like: ceiling(log2(number)). Of course, Visual FoxPro doesn't have a log2() function, but that can be solved easily as ceiling(log(number) / log(2))
As far as I see:
Thats seems to simple (was my first run)
ceiling(log2(x)) just gives you one out of two:
a)if only one bit is set, the bit number , log2(4) is 2, log2(8) is 3
b) if any lower bit is set, the number of the next higher bit. log2(7) is a little bit (sic!) lower then 3 while log2(5) is a bit more then 2: ceiling is 3 bit its 2 or 3 bits on
Lutz
Words are given to man to enable him to conceal his true feelings.
Charles Maurice de Talleyrand-Périgord
Weeks of programming can save you hours of planning.
OffThere is no place like [::1]