What are Left Shift and Right Shift Operators (>> and <<) in C#?

CsharpProgrammingServer Side Programming

Bitwise Left shift operator

The left operands value is moved left by the number of bits specified by the right operand.

Bitwise Right shift operator

The left operands value is moved right by the number of bits specified by the right operand.

The following is an example showing how to work with Bitwise left and right shift operators −

Example

 Live Demo

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();
      }
   }
}

Output

Value of c is 240
Value of c is 15
raja
Published on 07-Aug-2018 08:50:25
Advertisements