X hits on this document

750 views

0 shares

160 / 396

Chapter ‎18   Unsafe code

class Test { static void Main() { D cd1 = new D(C.M1); D cd2 = new D(C.M2); D cd3 = cd1 + cd2 + cd2 + cd1;// M1 + M2 + M2 + M1 cd3 -= cd1;// => M1 + M2 + M2

cd3 = cd1 + cd2 + cd2 + cd1;// M1 + M2 + M2 + M1 cd3 -= cd1 + cd2;// => M2 + M1

cd3 = cd1 + cd2 + cd2 + cd1;// M1 + M2 + M2 + M1 cd3 -= cd2 + cd2;// => M1 + M1

cd3 = cd1 + cd2 + cd2 + cd1;// M1 + M2 + M2 + M1 cd3 -= cd2 + cd1;// => M1 + M2

cd3 = cd1 + cd2 + cd2 + cd1;// M1 + M2 + M2 + M1 cd3 -= cd1 + cd1;// => M1 + M2 + M2 + M1 } }

## 7.8 Shift operators

The << and >> operators are used to perform bit shifting operations.

For an operation of the form x << count or x >> count, binary operator overload resolution (§‎7.2.4) is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.

When declaring an overloaded shift operator, the type of the first operand must always be the class or struct containing the operator declaration, and the type of the second operand must always be int.

The predefined shift operators are listed below.

Shift left:

int operator <<(int x, int count); uint operator <<(uint x, int count); long operator <<(long x, int count); ulong operator <<(ulong x, int count);

The << operator shifts x left by a number of bits computed as described below.

The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted left, and the low-order empty bit positions are set to zero.

Shift right:

int operator >>(int x, int count); uint operator >>(uint x, int count); long operator >>(long x, int count); ulong operator >>(ulong x, int count);

The >> operator shifts x right by a number of bits computed as described below.