Right-shift Operator (>>) Example C#

Right shift operator in C#: Here, we are going to learn about the right shift operator and how it works with C# program?
Submitted by Nidhi, on August 18, 2020 [Last updated : March 16, 2023]

Here we will create a program to demonstrate the right shift operation with different values.

C# program to demonstrate the example of the right-shift operator

The source code to demonstrate the right shift operations with different values in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the 
//example of right shift operator in C#.

using System;

class RightShiftDemo
{
    public static void Main()
    {
        int X = 128;
        int Y = 256;

        int R = 0;

        R = X >> 2;
        Console.WriteLine("X>>2 = " + R);

        R = Y >> 3;
        Console.WriteLine("Y>>3 = " + R);
    }
}

Output

X>>2 = 32
Y>>3 = 32
Press any key to continue . . .

Explanation

In the above program, we created a RightShiftDemo class that contains the Main() method, here we created three integer variables X, Y, and R initialized with 128, 256, and 0.

Now calculate the statements.

R = X >> 2;
R  =  128 / Power(2,2);
R  =  128 /( 2*2);
R  =  128/4;
R  = 32; 

Then,

R = Y >> 3;
R = 256 >> 3;
R = 256/ Power(2,3);
R = 256/(2*2*2);
R = 256/8;
R = 32;

C# Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.