C# 中的左移和右移運算子是什麼(>> 和 <<)?


位移左移運算子

左運算元的值向左移動由右運算元指定位數。

位移右移運算子

左運算元的值向右移動由右運算元指定位數。

以下示例顯示如何使用位移左移和右移運算子 -

示例

現場演示

using System;

namespace Demo {

   class Program {

      static void Main(string[] args) {

         int a = 60; /* 60 = 0011 1100 */
         int b = 13; /* 13 = 0000 1101 */
         int c = 0;

         c = a << 2; /* 240 = 1111 0000 */
         Console.WriteLine("Value of c is {0}", c);

         c = a >> 2; /* 15 = 0000 1111 */
         Console.WriteLine("Value of c is {0}", c);
         Console.ReadLine();
      }
   }
}

輸出

Value of c is 240
Value of c is 15

更新時間:20-6-2020

784 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.