www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generic test bit function (core.bitop)

reply Pierre <cathary.pierre neuf.fr> writes:
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.
Mar 06 2018
parent reply Alex <sascha.orlov gmail.com> writes:
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...
Mar 07 2018
parent Pierre <cathary.pierre neuf.fr> writes:
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.
Mar 08 2018