Has anyone prepared some general functions that extend bitmanipulations core.bitopto work on any type of value?
Sort of
bool getBit(T)(in T a, int bitnum);
T setBit(T)(in T a, int bitnum);
auto ref setBitInPlace(T)(ref T a, int bitnum);
I know that this is relatively easy to implement, so I wonder why it is not Phobos yet.
Update:
Here is my first attempt:
bool getBit(T, I)(in T a, I bitnum) @safe pure nothrow if (isIntegral!T &&
isIntegral!I) {
return a & (((cast(I)1) << bitnum)) ? true : false;
}
bool getBit(T, I)(in T a, I bitnum) @trusted pure nothrow if ((!(isIntegral!T)) &&
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;
return (*(cast(I*)&a)).getBit(bitnum);
}
alias bt = getBit;
My idea is to do the work getBitfor all types having semantics of values. That's why I need cast (I think). Are there any signs to check if the type has semantics of value or not?
Is there also an opportunity to check whether a type supports a specific operation, such as bitwise and &? I could always use __traits(compiles, ...), but standardization is good.
, , T, -, @safe ? cast @unsafe.
: http://forum.dlang.org/thread/tekrnzkemcbujbivvfpv@forum.dlang.org#post-tekrnzkemcbujbivvfpv:40forum.dlang.org