Has anybody cooked up some generic functions that extend
`core.bitop` bitmanipulations to work on any value type?
Something like
bool getBit(T)(in T a, int bitnum); // bt
T setBit(T)(in T a, int bitnum); // bts
auto ref setBitInPlace(T)(ref T a, int bitnum);
I know this is relatively easy to implement so that's why I'm
curious to why its not already Phobos.
**Update:**
Here's my first try at this:
/** Returns: $(D bitnum):th Bit Of $(D a).
TODO: Why is this already in Phobos?
TODO: Make this safe for integers
*/
bool getBit(T, I)(in T a, I bitnum) @trusted pure nothrow if
(isIntegral!I) {
enum nBits = 8*T.sizeof;
static if (nBits == 8) alias I = ubyte;
else static if (nBits == 16) alias I = ushort;
else static if (nBits == 32) alias I = uint;
else static if (nBits == 64) alias I = ulong;
immutable xi = (*(cast(I*)&a));
immutable bitMask = ((cast(I)1 << bitnum));
/* debug dln(eltIx, ",", bitnum, ",", xi, ",", bitMask);
*/
return a & bitMask ? true : false;
}
alias bt = getBit;
My idea to make `getBit` work on all types that have value
semantics. That's why I need the cast (I think). Is there a
traits to check if a type has value semantics or not?
Also is there a trait to check if a type supports a specific
operation such as bitwise and `&`? I could always use
`__traits(compiles, ...)` but standardization is good.
To make it even better I guess I need an explicit overload for
T's that support bit manipulations in order to make this variant
@safe right? In my generic solution above I need the `cast` and
that's @unsafe.
See also:
https://stackoverflow.com/questions/21368664/manipulating-bits-of-any-value-type