The bitwise operators ( & | ^ and ~ )

unsigned_int<> supports unsigned's bitwise operators & (bitand), | (bitor), ^ (bitxor) and unary ~ (compl).

The non-assigment binary operators ( &, |, ^ ) return and calculate with the promoted type of both arguments.

The assignment binary operators simply work on the result.

operator ~ () returns a complemented copy of its operand.

unsigned_int< 128 > a, b, d;
unsigned u;

unsigned_int< 128 > aAndB, aOrB, aXorB;

bitwise_init( a, b, aAndB, aOrB, aXorB );

CHECK( (a & b) == aAndB );
CHECK( (a | b) == aOrB );
CHECK( (a ^ b) == aXorB );

d = a;
a &= b;
CHECK( a == aAndB );
a = d;
a |= b;
CHECK( a == aOrB );
a = d;
a ^= b;
CHECK( a == aXorB );
a = d;

u = b;

CHECK( (a & u) == (unsigned)aAndB );
CHECK( (a | u) != (unsigned)aOrB && unsigned(a | u) == (unsigned)aOrB );
CHECK( (a ^ u) != (unsigned)aXorB && unsigned(a ^ u) == (unsigned)aXorB );
CHECK( (u & a) == (unsigned)aAndB );
CHECK( (u | a) != (unsigned)aOrB && unsigned(a | u) == (unsigned)aOrB );
CHECK( (u ^ a) != (unsigned)aXorB && unsigned(a ^ u) == (unsigned)aXorB );

d = a;
a &= u;
CHECK( a == (unsigned)aAndB );
a = d;
a |= u;
CHECK( a != (unsigned)aOrB && (unsigned)a == (unsigned)aOrB );
a = d;
a ^= u;
CHECK( a != (unsigned)aXorB && (unsigned)a == (unsigned)aXorB );
a = d;

u = d;
u &= b;
CHECK( u == (unsigned)aAndB );
u = d;
u |= b;
CHECK( u == (unsigned)aOrB );
u = d;
u ^= b;
CHECK( u == (unsigned)aXorB );

/* check op ~ too
*/
CHECK( ~a == ~a && !(~a ^ ~a) );
a = -1;
CHECK( !~a );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

Ok