C# 中的左移和右移运算符(>> 和 << )是什么?

csharpprogrammingserver side programming更新于 2025/10/15 16:07:17

按位左移运算符

将左操作数的值左移,移动的位数由右操作数指定。

按位右移运算符

将左操作数的值右移,移动的位数由右操作数指定。

以下示例展示了如何使用按位左移和右移运算符 −

示例

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

相关文章