Number of Bits Needed to Represent a Zero-Offset Integer
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 19 05:30:47 PST 2015
On 1/19/15 7:04 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?=
<per.nordlow at gmail.com>" wrote:
> As a follow-up to
>
> http://forum.dlang.org/thread/fdfwrdtjcawprvvkoqas@forum.dlang.org#post-qxudiyoygnvvbovhjfgt:40forum.dlang.org
>
>
> I'm looking for a function that figures out the number of bits that are
> needed to represent a zero-based integer:
>
> value-set => bits
> =================
> 0,1 => 1 (*)
> 0,1,2 => 2
> 0,1,2,3 => 2 (*)
> 0,1,2,3,4 => 3
> 0,1,2,3,4,5 => 3
> 0,1,2,3,4,5,6 => 3
> 0,1,2,3,4,5,6,7 => 3 (*)
> 0,1,2,3,4,5,6,7,8 => 4
> ....
>
> (*) means full bit usage
http://dlang.org/phobos/core_bitop.html#.bsr
It's actually an intrinsic, reduces to an instruction. Mind the
requirements for 0.
-Steve
More information about the Digitalmars-d-learn
mailing list