How to use shift operators in C#

Using shift operators

<< is the left shift operator and >> is the right shift operator.

The general forms for these operators are shown here:


value < num-bits 
value > num-bits 

A 'left shift' shift all bits to the left and a zero bit to be brought in on the right. A 'right shift' causes all bits to be shifted right one position.

In the case of a right shift on an unsigned value, a zero is brought in on the left. In the case of a right shift on a signed value, the sign bit is preserved.

left shift


class MainClass/*from   w w  w.ja  v a  2s.  c om*/
{

  public static void Main()
  {

    byte byte1 = 0x9a;  // binary 10011010, decimal 154
    byte byte2 = 0xdb;  // binary 11011011, decimal 219
    byte result;

    System.Console.WriteLine("byte1 = " + byte1);
    System.Console.WriteLine("byte2 = " + byte2);

    
    result = (byte) (byte1 << 1);
    System.Console.WriteLine("byte1 << 1 = " + result);
  }

}

The code above generates the following result.


using System; /*from   ww  w .  j  av  a  2 s  . com*/
 
class Example { 
  public static void Main() { 
    int val = 1; 
 
    for(int i = 0; i < 8; i++) {  
      for(int t=128; t > 0; t = t/2) { 
        if((val & t) != 0) Console.Write("1 ");  
        if((val & t) == 0) Console.Write("0 ");  
      } 
      Console.WriteLine(); 
      val = val << 1; // left shift 
    } 
    Console.WriteLine(); 
 
  } 
}

The code above generates the following result.

right shift


class MainClass/* www.  j a  v  a2s  .co m*/
{

  public static void Main()
  {

    byte byte1 = 0x9a;  // binary 10011010, decimal 154
    byte byte2 = 0xdb;  // binary 11011011, decimal 219
    byte result;

    System.Console.WriteLine("byte1 = " + byte1);
    System.Console.WriteLine("byte2 = " + byte2);

    
    result = (byte) (byte1 >> 1);
    System.Console.WriteLine("byte1 >> 1 = " + result);
  }

}

The code above generates the following result.


using System; /*from   w  ww  .  j  av a  2  s . c om*/
 
class Example { 
  public static void Main() { 
    int val = 128; 
 
    for(int i = 0; i < 8; i++) {  
      for(int t=128; t > 0; t = t/2) { 
        if((val & t) != 0) Console.Write("1 ");  
        if((val & t) == 0) Console.Write("0 ");  
      } 
      Console.WriteLine(); 
      val = val >> 1; // right shift 
    } 
  } 
}

The code above generates the following result.

Use the shift operators to multiply and divide by 2


using System; /*from  www . jav a 2s .com*/
 
class Example {  
  public static void Main() { 
    int n; 
 
    n = 10; 
 
    Console.WriteLine("Value of n: " + n); 
 
    Console.WriteLine("multiply by 2");
    n = n << 1; 
    Console.WriteLine("Value of n after n = n * 2: " + n); 
 
    Console.WriteLine("multiply by 4"); 
    n = n << 2; 
    Console.WriteLine("Value of n after n = n * 4: " + n); 
 
    Console.WriteLine("divide by 2");
    n = n >> 1; 
    Console.WriteLine("Value of n after n = n / 2: " + n); 
 
    Console.WriteLine("divide by 4");
    n = n >> 2; 
    Console.WriteLine("Value of n after n = n / 4: " + n); 
    Console.WriteLine(); 
 
    Console.WriteLine("reset n");
    n = 10; 
    Console.WriteLine("Value of n: " + n); 
 
    Console.WriteLine("multiply by 2, 30 times");
    n = n << 30; // data is lost 
    Console.WriteLine("Value of n after left-shifting 30 places: " + n); 
 
  } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception