>> 運算子 (C# 參考)>> operator (C# Reference)

右移運算子 (>>) 會向其第一個運算元右移第二個運算元所定義的位元數。The right-shift operator >> shifts its first operand right by the number of bits defined by its second operand. 所有整數型別都支援 >> 運算子。All integer types support the >> operator. 不過,第二個運算元的類型必須是 int 或對 int 進行預先定義隱含數值轉換的類型。However, the type of the second operand must be int or a type that has a predefined implicit numeric conversion to int.

右移作業會捨棄低位位元。The right-shift operation discards the low-order bits. 會根據第一個運算元的類型來設定高位空位元位置,如下所示:The high-order empty bit positions are set based on the type of the first operand as follows:

  • 若第一個運算元的型別為 intlong,則右移運算元會執行算術位移:第一個運算元之最高有效位元 (正負號位元) 的值會傳播到高為空位元位置。If the first operand is of type int or long, the right-shift operator performs an arithmetic shift: the value of the most significant bit (the sign bit) of the first operand is propagated to the high-order empty bit positions. 也就是說,若第一個運算元不是負值且在為負值時被設定為一,則高位空位元位置會被設定為零。That is, the high-order empty bit positions are set to zero if the first operand is non-negative and set to one if it's negative.

  • 若第一個運算元的型別為 uintulong,則右移運算子會執行邏輯位移:高位空位元位置一律會被設定為零。If the first operand is of type uint or ulong, the right-shift operator performs a logical shift: the high-order empty bit positions are always set to zero.

下列範例示範了該行為:The following example demonstrates that behavior:

uint x = 0b_1001;
Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2), 4}");

uint y = x >> 2;
Console.WriteLine($"After:  {Convert.ToString(y, toBase: 2), 4}");
// Output:
// Before: 1001
// After:    10

int a = int.MinValue;
Console.WriteLine($"Before: {Convert.ToString(a, toBase: 2)}");

int b = a >> 3;
Console.WriteLine($"After:  {Convert.ToString(b, toBase: 2)}");
// Output:
// Before: 10000000000000000000000000000000
// After:  11110000000000000000000000000000

位移計數Shift count

針對運算式 x >> count,實際位移計數相依於 x 的型別,如下所示:For the expression x >> count, the actual shift count depends on the type of x as follows:

  • x 的型別為 intuint,則位移計數是由第二個運算元的低位五個位元所給定。If the type of x is int or uint, the shift count is given by the low-order five bits of the second operand. 也就是說,位移計數是從 count & 0x1F (或 count & 0b_1_1111) 所計算。That is, the shift count is computed from count & 0x1F (or count & 0b_1_1111).

  • x 的型別為 longulong,則位移計數是由第二個運算元的低位六個位元所給定。If the type of x is long or ulong, the shift count is given by the low-order six bits of the second operand. 也就是說,位移計數是從 count & 0x3F (或 count & 0b_11_1111) 所計算。That is, the shift count is computed from count & 0x3F (or count & 0b_11_1111).

下列範例示範了該行為:The following example demonstrates that behavior:

int a = 0b_0100;
int count1 = 0b_0000_0001;
int count2 = 0b_1110_0001;
Console.WriteLine($"{a} >> {count1} is {a >> count1}; {a} >> {count2} is {a >> count2}");
// Output:
// 4 >> 1 is 2; 4 >> 225 is 2

備註Remarks

位移作業一律不會導致溢位並產生已選取與未選取內容的相同結果。Shift operations never cause overflows and produce the same results in checked and unchecked contexts.

運算子是否可多載Operator overloadability

使用者定義型別可以多載 >> 運算子。User-defined types can overload the >> operator. 若使用者定義型別 T 會多載 >> 運算子,則第一個運算元的型別必須是 T,且第二個運算元的型別必須是 intIf a user-defined type T overloads the >> operator, the type of the first operand must be T and the type of the second operand must be int. 多載 >> 運算子時,右移指派運算子 >>=也會隱含地多載。When the >> operator is overloaded, the right-shift assignment operator >>= is also implicitly overloaded.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格位移運算子一節。For more information, see the Shift operators section of the C# language specification.

另請參閱See also