这几天在网上看到几篇关于VB位运算符的帖子,有篇文章叫《VB移位运算函数》的,代码不仅繁琐、效率低,而且存在漏洞,在网上被四处转载,有些误人子弟。为此,写了几个关于VB位运算的函数,以供大家学习交流:
'位左移 Public Function SHL(nSource As Long,n As Byte) As Long SHL = nSource * 2 ^ n End Function '位右移 Public Function SHR(nSource As Long,n As Byte) As Long SHR = nSource / 2 ^ n End Function '获得指定的位 Public Function GetBits(nSource As Long,n As Byte) As Boolean GetBits = nSource And 2 ^ n End Function '设置指定的位 Public Function SetBits(nSource As Long,n As Byte) As Long SetBits = nSource Or 2 ^ n End Function '清除指定的位 Public Function ResetBits(nSource As Long,n As Byte) As Long ResetBits = nSource And Not 2 ^ n End Function