Thread overview |
---|
March 06, 2018 Generic test bit function (core.bitop) | ||||
---|---|---|---|---|
| ||||
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 Re: Generic test bit function (core.bitop) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pierre | 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 Re: Generic test bit function (core.bitop) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | 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.
|
Copyright © 1999-2021 by the D Language Foundation