VB中位操作运算函数
VB中位操作运算函数【移位指令】
'Module: BitPlus.Bas
'Code By Hermit @ SMTH , Jun. 1st,2000
'Email:*****************
'May these functions will help you, and
'Please keep this header if you use my code,thanks!
'提供在VB下进行位运算的函数
'SHL 逻辑左移 SHR 逻辑右移
'SAL 算术左移 SAR 算术右移
'ROL 循环左移 ROR 循环右移
'RCL 带进位循环左移 RCR 带进位循环右移
'Bin 将给定的数据转化成2进制字符串
'使用方法
'SHL SHR SAL SAR ROL ROR 基本类似,以SHL为例说明
'可以移位的变量类型,字节(Byte),整数(Integer),长整数(Long)
'返回值 True 移位成功, False 移位失败,当对非上述类型进行移位是会返回False
'Num 传引用变量,要移位的数据,程序会改写Num的值为运算后结果
'iCL 传值变量,要移位的次数,缺省值移位1次
'例 Dim A As Integer
' A = &H10
'如 SHL A 则移位后 A = &H20
'如 SHL A,2 则移位后 A = &H40
'如 SHL A,4 则移位后 A = &H00
'RCR与RCL类似,以RCL为例说明
'这里需要多给定一个参数,即第一次移位时的进位值iCF
'Bin举例
'A = &H1
'如 A 为字节,则 Bin(A) 返回值为 "00000001"
'如 A 为整数,则 Bin(A) 返回值为 "0000000000000001"vb编程基础知识函数
'如 A 为长整数,则 Bin(A) 返回值为"00000000000000000000000000000001"
'如果传入参数非上述类型时,返回值为 ""
'更详细的信息,请参考相关汇编书籍
'逻辑左移
Public Function SHL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As
Boolean
Dim i As Byte
Dim bMask As Byte, iMask As Integer, lMask As Long
Select Case VarType(Num)
Case 2 '16 bits
For i = 1 To iCL
iMask = 0
If (Num And &H4000) <> 0 Then iMask = &H8000
Num = (Num And &H3FFF) * 2 Or iMask
Next
Case 3 '32 bits
For i = 1 To iCL
lMask = 0
If (Num And &H40000000) <> 0 Then lMask = &H80000000
Num = (Num And &H3FFFFFFF) * 2 Or lMask
Next
Case 17 '8 bits
For i = 1 To iCL
bMask = 0
If (Num And &H40) <> 0 Then bMask = &H80
Num = (Num And &H3F) * 2 Or bMask
Next
Case Else
SHL = False
Exit Function
End Select
SHL = True
End Function
'逻辑右移
Public Function SHR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As
Boolean
Dim i As Byte
Dim bMask As Byte, iMask As Integer, lMask As Long
Select Case VarType(Num)
Case 2 '16 bits
For i = 1 To iCL
iMask = 0
If (Num And &H8000) <> 0 Then iMask = &H4000
Num = (Num And &H7FFF) \ 2 Or iMask
Next
Case 3 '32 bits
For i = 1 To iCL
lMask = 0
If (Num And &H80000000) <> 0 Then lMask = &H40000000
Num = (Num And &H7FFFFFFF) \ 2 Or lMask
Next
Case 17 '8 bits
For i = 1 To iCL
bMask = 0
If (Num And &H80) <> 0 Then bMask = &H40
Num = (Num And &H7F) \ 2 Or bMask
Next
Case Else
SHR = False
Exit Function
End Select
SHR = True
End Function
'算术左移
Public Function SAL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As
Boolean
SAL = SHL(Num, iCL)
End Function
'算术右移
Public Function SAR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As
Boolean
Dim i As Byte
Dim bMask As Byte, iMask As Integer, lMask As Long
Select Case VarType(Num)
Case 2 '16 bits
For i = 1 To iCL
iMask = 0
If (Num And &H8000) <> 0 Then iMask = &HC000
Num = (Num And &H7FFF) \ 2 Or iMask
Next
Case 3 '32 bits
For i = 1 To iCL
If (Num And &H80000000) <> 0 Then lMask = &HC0000000
Num = (Num And &H7FFFFFFF) \ 2 Or lMask
Next
Case 17 '8 bits
For i = 1 To iCL
If (Num And &H80) <> 0 Then bMask = &HC0
Num = (Num And &H7F) \ 2 Or bMask
Next
Case Else
SAR = False
Exit Function
End Select
SAR = True
End Function
'循环左移
Public Function ROL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As
Boolean
Dim i As Byte
Dim bMask As Byte, iMask As Integer, lMask As Long
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论