Sample Shift Left and Shift Right Operator in C#

In this post, I would like to show about shift left and shift right operator in C#.

1. Shift left operator:  x<< y = x * 2 ^y  

   private void btnAnswerLeft_Click(object sender, EventArgs e)

   {
       int x = 2;
       int y = 3;
       int result;
       result = (x << y);
       // 2 << 3 ==> 2 * 2^3 =16 
      MessageBox.Show(result,"Answer,MessageBoxButtons.OK, 
      MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
   }

1. Shift Right operator:  x>> y = x / 2 ^y   

 private void btnAnswerLeft_Click(object sender, EventArgs e)

{


    int x = 8;
    int y = 2;
    int result;
    result = (x >> y);
    // 8 >> 2 ==> 8 / 2^2 =2
    MessageBox.Show(result,"Answer",MessageBoxButtons.OK,
    MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
 }

Click here to download source code
Shift Left and Shift Right operator 

Sample Shift Left and Shift Right Operator in C# Sample Shift Left and Shift Right Operator in C# Reviewed by BeiLover on 6:15 PM Rating: 5

No comments:

Powered by Blogger.