课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 移位运算符

当前位置:免费教程 » 程序设计 » VB.Net

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

运算符描述示例
AndBitwise AND Operator copies a bit to the result if it exists in both operands.
如果两个操作数都存在,则位与运算符将一个位复制到结果。
(A AND B) will give 12, which is 0000 1100
OrBinary OR Operator copies a bit if it exists in either operand.
二进制OR运算符复制一个位,如果它存在于任一操作数。
(A Or B) will give 61, which is 0011 1101
XorBinary XOR Operator copies the bit if it is set in one operand but not both.
二进制XOR运算符复制该位,如果它在一个操作数中设置,但不是两个操作数。
(A Xor B) will give 49, which is 0011 0001
NotBinary Ones Complement Operator is unary and has the effect of 'flipping' bits.
二进制补码运算符是一元的,具有“翻转”位的效果。
(Not A ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number.
<<Binary Left Shift Operator. The left operand's value is moved left by the number of bits specified by the right operand.
二进制左移位运算符。 左操作数的值向左移动由右操作数指定的位数。
A << 2 will give 240, which is 1111 0000
>>Binary Right Shift Operator. The left operand's value is moved right by the number of bits specified by the right operand.
二进制右移运算符。 左操作数的值向右移动由右操作数指定的位数。
A >> 2 will give 15, which is 0000 1111

尝试以下示例来了解VB.Net中提供的所有位运算符:

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

当上述代码被编译和执行时,它产生以下结果:

  1. Line 1 - Value of c is 12
  2. Line 2 - Value of c is 61
  3. Line 3 - Value of c is 49
  4. Line 4 - Value of c is -61
  5. Line 5 - Value of c is 240
  6. Line 6 - Value of c is 15
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号