Generic test bit function (core.bitop)
Alex
sascha.orlov at gmail.com
Wed Mar 7 13:26:06 UTC 2018
On Tuesday, 6 March 2018 at 10:37:30 UTC, Pierre wrote:
> Hi all,
>
> I would like to use bt function (core.bitop) on generic array
> but it seems that's not possible. I would like to know if there
> is some reasons to have a fixed type (size_t) instead of
> something like :
>
> pure @system int bt(T)(in T* p,size_t bitnum)
> if(__traits(isIntegral,T))
> {
> return p[bitnum/ (T.sizeof*8)] & (1 << (bitnum&
> ((T.sizeof*8) - 1)));
> }
>
> Thank you for your help.
From this point of view, size_t is not fixed, but capable to
point to any place in memory.
Therefore, pointer of any type have by definition exactly the
defined size of size_t.
If you want to use an array (or an object of custom type) as a
bit array, there is for example
https://dlang.org/library/std/bitmanip/bit_array.html
which fits more, I think...
More information about the Digitalmars-d-learn
mailing list