目录

Show 例子

假设变量A保持60而变量B保持13,则 -

操作者 描述
And 如果两个操作数中都存在,则按位AND运算符将一个位复制到结果中。 (A和B)将给出12,即0000 1100
Or 二进制OR运算符如果存在于任一操作数中,则复制一位。 (A或B)将给出61,即0011 1101
Xor 二进制异或运算符如果在一个操作数中设置但不在两个操作数中设置,则复制该位。 (A Xor B)将给出49,即0011 0001
Not 二元一元补语运算符是一元的,具有“翻转”位的效果。 (不是A)将给出-61,由于带符号的二进制数,它是2的补码形式的1100 0011。
<< 二进制左移运算符。 左操作数的值向左移动右操作数指定的位数。 A << 2将给出240,即1111 0000
>> 二进制右移运算符。 左操作数的值向右移动右操作数指定的位数。 A >> 2将给出15,即0000 1111

尝试以下示例以了解VB.Net中可用的所有按位运算符 -

Module BitwiseOp
   Sub Main()
      Dim a As Integer = 60       ' 60 = 0011 1100   
      Dim b As Integer = 13       ' 13 = 0000 1101
      Dim c As Integer = 0
      c = a And b       ' 12 = 0000 1100 
      Console.WriteLine("Line 1 - Value of c is {0}", c)
      c = a Or b       ' 61 = 0011 1101 
      Console.WriteLine("Line 2 - Value of c is {0}", c)
      c = a Xor b       ' 49 = 0011 0001 
      Console.WriteLine("Line 3 - Value of c is {0}", c)
      c = Not a          ' -61 = 1100 0011 
      Console.WriteLine("Line 4 - Value of c is {0}", c)
      c = a << 2     ' 240 = 1111 0000 
      Console.WriteLine("Line 5 - Value of c is {0}", c)
      c = a >> 2    ' 15 = 0000 1111 
      Console.WriteLine("Line 6 - Value of c is {0}", c)
      Console.ReadLine()
   End Sub
End Module

编译并执行上述代码时,会产生以下结果 -

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
↑回到顶部↑
WIKI教程 @2018