C# Bitwise Shift Operators

In this chapter you will learn:

  1. How to use shift operators
  2. left shift
  3. right shift
  4. Use the shift operators to multiply and divide by 2

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//  w  ww. j  a  v a  2s  .  c o 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  w w. ja va  2  s  .  co  m*/
 
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/*  w  w w .jav a2 s.  c  o 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   ww w.java  2  s  .  c o  m
 
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  w w w .ja  va  2  s .  c  o  m
 
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.

Next chapter...

What you will learn in the next chapter:

  1. How use sizeof operator to find out the size of a type in bytes
  2. Syntax for sizeof operator
  3. Example for sizeof operator
Home »
  C# Tutorial »
    C# Language »
      C# Operators
C# Arithmetic Operators
C# Arithmetic Assignment Operator
C# Increment and Decrement Operators
C# Remainder operator
C# Relational operators
C# Conditional Operators
C# Ternary operator(The ? Operator)
C# Bitwise Operators
C# Bitwise Shift Operators
C# sizeof Operator
C# typeof Operator