Shift Operators

sint2t is signed_int< 64 > on most 32 bit machines.

Contents

Shift Left (<<)

sint2t mul =  sint2t( 1 ) << (UINT_BITS - 1);
sint2t a = 0x1A50, c, r;

c = a << (UINT_BITS - 1);
r = c / mul;

CHECK_PRINT( a == r, hex << r << ", " << a );

cout << hex << "(" << a << " << " << dec << (UINT_BITS - 1) << ") = " << hex << c << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(1a50 << 31) = d2800000000
Ok

Assign (<<=)

sint2t mul =  sint2t( 1 ) << (UINT_BITS - 1);
sint2t a = 0x1A50, c, r;

c = a;
c <<= (UINT_BITS - 1);
r = c / mul;

CHECK_PRINT( a == r, hex << r << ", " << a );

cout << hex << "(" << a << " <<= " << dec << (UINT_BITS - 1) << ") = " << hex << c << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(1a50 <<= 31) = d2800000000
Ok

Shift Right (>>)

sint2t mul =  sint2t( 1 ) << (UINT_BITS - 1);
sint2t a = 0x1A50, c;

c = (a * mul) >> (UINT_BITS - 1);

CHECK_PRINT( a == c, hex << c << ", " << a );

cout << hex << "(" << (a * mul) << " >> " << dec << (UINT_BITS - 1) << ") = " << hex << c << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(d2800000000 >> 31) = 1a50
Ok

Negative (>>)

Check the sign bit('s) doesn't get lost, i.e. that it is an arithmetic shift right.

sint2t mul =  sint2t( 1 ) << (UINT_BITS - 1);
sint2t a = -0x1A50, c;

c = (a * mul) >> (UINT_BITS - 1);

CHECK_PRINT( a == c, hex << c << ", " << a );

cout << hex << "(" << (a * mul) << " >> " << dec << (UINT_BITS - 1) << ") = " << hex << c << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(fffff2d800000000 >> 31) = ffffffffffffe5b0
Ok

Assign (>>=)

sint2t mul =  sint2t( 1 ) << (UINT_BITS - 1);
sint2t a = 0x1A50, c;

c = (a * mul);
c >>= (UINT_BITS - 1);

CHECK_PRINT( a == c, hex << c << ", " << a );

cout << hex << "(" << (a * mul) << " >>= " << dec << (UINT_BITS - 1) << ") = " << hex << c << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(d2800000000 >>= 31) = 1a50
Ok