On Monday, 19 January 2015 at 21:23:47 UTC, Nordlöw wrote:
On Monday, 19 January 2015 at 20:54:50 UTC, Steven
Schveighoffer wrote:
Cool. I would point out that the commented code suggests you
should be handling the 0 case, but you are not (when T.min ==
T.max)
I believe that should trigger a failing static assert with a
good error message as it doesn't make any sense to call the
function in that case. Thanks.
I would recommend to use something like this:
/// returns the number of the highest set bit +1 in the given
value or 0 if no bit is set
size_t bitlen(T)(const(T) a) pure @safe @nogc nothrow
if(isUnsigned!T)
{
static if(T.sizeof <= size_t.sizeof) // doesn't work for ulong
on 32bit sys
{
return x ? core.bitop.bsr(x)+1 : 0;
}
else static if(T.sizeof == 8) // ulong if size_t == uint
{
return x ? x>>32 ? core.bitop.bsr(x)+33 :
core.bitop.bsr(x)+1 : 0;
}
}