如何在 C# 中使用右移運算子?


右移運算子中,左運算元的值根據右運算元的位數向右移動。

讓我們看一個 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("Right Shift Operator - Value of b is {0}", b);
         Console.ReadLine();
      }
   }
}

上面,a 的值為 60,即二進位制中的 0011 1100。

如上例所示設定右移運算子。這將位向右移動兩次 -

a >> 2

現在輸出將為 15,即。

15 = 0000 1111

更新於: 20-6 月-2020

115 次瀏覽

開啟你的事業

完成課程後獲得認證

開始
廣告