Number of Bits Needed to Represent a Zero-Offset Integer

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 14 00:01:27 PST 2015


On Friday, 13 February 2015 at 22:39:10 UTC, bearophile wrote:
> H. S. Teoh:
>
>> So it could be called ilog2?
>
> Perhaps floorIlog2? Isn't ilog2 a different function?
>
> Bye,
> bearophile

I think the naming depends on use context, so it is reasonable to
land on different names in different domains. Maybe the standard
notation should be ffs(x):

http://en.wikipedia.org/wiki/Find_first_set

I like to think of it as position of most significant bit,
msbpos(x), but the trouble with non "log2" names is that you have
to decide wether to count from 0 or 1...

The advantage with counting from 1 is that ffs(0) could be 0,
whereas with counting from 0 you need to return -1 or fail...

Maybe one should have both, "log2" that fails and a msb-counting
one that does not fail.


More information about the Digitalmars-d-learn mailing list