| [ directory ] |
|
8.6 Shift Operators8.6.1 The << OperatorThe left-shift operator (<<) shifts its LHS operand left by the number of bits specified by its RHS operand: expr << count Here, expr is the expression of type int, uint, long, or ulong and is the value to be shifted; count is the shift count and must be an int. If expr is an int or uint (a 32-bit quantity), the shift count is given by the low-order five bits of count (count & 0x1f). If expr is a long or ulong (a 64-bit quantity), the shift count is given by the low-order six bits of count (count & 0x3f). The high-order bits of expr are discarded, and the low-order empty bits are zero-filled. 8.6.2 The >> OperatorThe right-shift operator (>>) shifts its LHS operand right by the number of bits specified by its RHS operand: expr >> count Here, expr is an expression of type int, uint, long, or ulong and is the value to be shifted; count is the shift count and must be an int. If expr is an int or uint (a 32-bit quantity), the shift count is given by the low-order five bits of count (count & 0x1f). If expr is a long or ulong (a 64-bit quantity), the shift count is given by the low-order six bits of count (count & 0x3f). If expr is an int or long, the right-shift is an arithmetic shift (high-order empty bits are set to the sign bit). If expr is of type uint or ulong, the right-shift is a logical shift (high-order bits are zero-filled). Listing 8.6 shows shift operations. Listing 8.6 Shift Operations (C#)
using System;
public class ShiftTest {
public static void Main(string[] args) {
int i = 1;
int j = 33;
Console.WriteLine(i << j);
Console.WriteLine(i >> j);
}
}
Here is the output of Listing 8.6: 2 0 Shift operations never cause overflows. The << and >> operators can be overloaded. |
| [ directory ] |
|