Thread overview
Generic test bit function (core.bitop)
Mar 06, 2018
Pierre
Mar 07, 2018
Alex
Mar 08, 2018
Pierre
March 06, 2018
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.
March 07, 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...
March 08, 2018
On Wednesday, 7 March 2018 at 13:26:06 UTC, Alex wrote:
> 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.

Thank you,I thougth that pointer aliasing wasn't allowed.