How to use RightShift Operators in C#?


The left operands value is moved right by the number of bits specified by the right operand in Right Shift Operator.

Let us see an example of Right Shift operator in 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();
      }
   }
}

Above, the value of a is 60 i.e. 0011 1100 in binary.

Set right shift operator as shown in the above example. This shifts the bits to the right twice −

a >> 2

Now the output will be 15 i.e.

15 = 0000 1111

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements