# 位操作 – 操纵任何值类型的位

``````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);
``````

``````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); // reuse integer variant
}
alias bt = getBit;
``````

Is there a traits to check if a type has value semantics or not?

``````import std.stdio;

struct Foo {}

auto fn(T)(T type)
{
static if (is(T == struct)) {
writeln(T.stringof ~ " is a value type");
} else if (is(T == class)) {
writeln(T.stringof ~ " is a reference type");
} else {
writeln(T.stringof ~ " is something else");
}
}

void main()
{
Foo f;

fn(f);
fn(new Object());
fn(1);
}
``````

Also is there a trait to check if a type supports a specific operation
such as bitwise and &?

``````import std.stdio;

struct Foo {
auto opBinary(string op)(Foo rhs)
if (op == "&")
{
return rhs.init; // dummy implementation
}
};

template canUseBitOps(T)
{
enum bool canUseBitOps = is(typeof(
(inout int = 0)
{
T t1 = T.init;
T t2 = T.init;
auto r = t1 & t2;
}));
}

void main()
{
assert(canUseBitOps!int);
assert(!canUseBitOps!string);
assert(canUseBitOps!Foo);
}
``````