Csharp/CSharp Tutorial/Data Type/Bitwise Shift

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

left shift

class MainClass
{
  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);
  }
}
byte1 = 154
byte2 = 219
byte1 << 1 = 52

>> operators

using System; 
 
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 
    } 
  } 
}
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1

right shift

class MainClass
{
  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);
  }
}
byte1 = 154
byte2 = 219
byte1 >> 1 = 77

The shift

using System; 
 
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(); 
 
  } 
}
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0

Use the shift operators to multiply and divide by 2

using System; 
 
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); 
 
  } 
}
Value of n: 10
multiply by 2
Value of n after n = n * 2: 20
multiply by 4
Value of n after n = n * 4: 80
divide by 2
Value of n after n = n / 2: 40
divide by 4
Value of n after n = n / 4: 10
reset n
Value of n: 10
multiply by 2, 30 times
Value of n after left-shifting 30 places: -2147483648