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

• 若第一個運算元的型別為 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.

``````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` 的型別為 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`).

``````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
``````