Shift Operators

Much less commonly used than arithmetic operators are the operators that are tightly bound to the underlying bit representation of integral types. One such category consists of shift operators >> and <<, respectively known as left shift and right shift. Shift operators are defined for the int, uint, long, and ulong types but can also be used with other integral types because of implicit conversions.

The following are examples of shift operations:

int left = 3;int lres = left << 5; // 96int right = 42;int rres = right >> 3; // 5

Both operators take two operands. The one on the left takes the expression that needs to be shifted, and the one on the right specifies the number of bits to shift (specified as an int). The operation ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.