如何在 C# 中使用右移运算符?

csharpprogrammingserver side programming更新于 2025/7/13 6:52:17

右移运算符将左操作数的值向右移动右操作数指定的位数。

让我们看一个 C# 右移运算符 − 的示例

using System;

namespace OperatorsAppl {

   class Program {

      static void Main(string[] args) {
         int a = 60; /* 60 = 0011 1100 */
         int b = 0;

         b = a >> 2; /* 15 = 0000 1111 */
         Console.WriteLine("右移运算符 - b 的值为 {0}", b);
         Console.ReadLine();
      }
   }
}

以上代码中,a 的值为 60,即二进制的 0011 1100。

如上例所示设置右移运算符。这将位向右移动两次 −

a >> 2

现在输出为 15,即

15 = 0000 1111

相关文章