Bitwise right shift operators in C#

CsharpServer Side ProgrammingProgramming

Bitwise operator works on bits and performs bit by bit operation. In Bitwise right shift operator the value of the left operand is moved right by the number of bits specified by the right operand.

In the below code, we have the value −

60 i.e. 0011 1100

On the right shift %minus;

c = a >> 2;

It converts into 15 after right shift twice −

15 i.e. 0000 1111

Example

You can try to run the following code to implement Bitwise right shift operator in C# −

using System;
using System.Collections.Generic;
using System.Text;
namespace Demo {
   class toBinary {
      static void Main(string[] args) {
         int a = 60;    /* 60 = 0011 1100 */
         int b = 0;
         c = a >> 2;    /* 15 = 0000 1111 */
         Console.WriteLine("Value of b is {0}", b);
         Console.ReadLine();
      }
   }
}
raja
Published on 24-Jul-2018 09:23:32
Advertisements