[vb]代码库
'功能: 将字节变量循环左移n位
'入口参数:MoveByte 需要移位的字节变量
' n 移动的位数
'返回值: 移位后的字节变量
Public Function ByteLeftMove(MoveByte As Byte, N As Integer) As Byte
Dim Intem As Byte '临时变量
Dim Intem1 As Byte '临时变量
Dim x As Integer, y As Integer
Intem1 = MoveByte
For x = 1 To N '移多少位就循环多少次
For y = 8 To 1 Step -1 '从第八位(左边第一位)开始循环左移
Select Case y
Case 8
'如果临时变量intem1的第八位是1,
If (Intem1 And &H80) = &H80 Then
'则将临时变量intem置1,
Intem = &H1
Else
Intem = &H0 '反之置0
End If
Case 7
'如果临时变量intem1的第七位是1,
If (Intem1 And &H40) = &H40 Then
'则将其第八位置1(其它位不变),
Intem1 = Intem1 Or &H80
Else
'反之将第八位置0(其它位不变)
Intem1 = Intem1 And &H7F
End If
Case 6
'操作与上面相同
If (Intem1 And &H20) = &H20 Then
Intem1 = Intem1 Or &H40
Else
Intem1 = Intem1 And &HBF
End If
Case 5
If (Intem1 And &H10) = &H10 Then
Intem1 = Intem1 Or &H20
Else
Intem1 = Intem1 And &HDF
End If
Case 4
If (Intem1 And &H8) = &H8 Then
Intem1 = Intem1 Or &H10
Else
Intem1 = Intem1 And &HEF
End If
Case 3
If (Intem1 And &H4) = &H4 Then
Intem1 = Intem1 Or &H8
Else
Intem1 = Intem1 And &HF7
End If
Case 2
If (Intem1 And &H2) = &H2 Then
Intem1 = Intem1 Or &H4
Else
Intem1 = Intem1 And &HFB
End If
Case 1
If (Intem1 And &H1) = &H1 Then
Intem1 = Intem1 Or &H2
Else
Intem1 = Intem1 And &HFD
End If
If Intem = &H1 Then '移完第一位后,如果intem是1(即第八位是1)
Intem1 = Intem1 Or &H1 '则将intem1的第一位置1
Else
Intem1 = Intem1 And &HFE '反之置0
End If
End Select
Next y
Next x
ByteLeftMove = Intem1 '将intem1的值返回给函数名
End Function
'功能: 将字节变量循环右移n位
'入口参数:MoveByte 需要移位的字节变量
' n 移动的位数
'返回值: 移位后的字节变量
Public Function ByteRightMOve(MoveByte As Byte, N As Integer) As Byte
Dim Intem As Byte '临时变量
Dim Intem1 As Byte '临时变量
Dim x As Integer, y As Integer
Intem1 = MoveByte
For x = 1 To N '移多少位就循环多少次
For y = 1 To 8 Step 1 '从第一位(右边第一位)开始循环右移
Select Case y
Case 1
'如果临时变量intem1的第一位是1,
If (Intem1 And &H1) = &H1 Then
'则将临时变量intem置1,
Intem = &H1
Else
Intem = &H0 '反之置0
End If
Case 2
'如果临时变量intem1的第二位是1,
If (Intem1 And &H2) = &H2 Then
'则将其第一位置1(其它位不变),
Intem1 = Intem1 Or &H1
Else
'反之将第一位置0(其它位不变)
Intem1 = Intem1 And &HFE
End If
Case 3
'操作与上面相同
If (Intem1 And &H4) = &H4 Then
Intem1 = Intem1 Or &H2
Else
Intem1 = Intem1 And &HFD
End If
Case 4
If (Intem1 And &H8) = &H8 Then
Intem1 = Intem1 Or &H4
Else
Intem1 = Intem1 And &HFB
End If
Case 5
If (Intem1 And &H10) = &H10 Then
Intem1 = Intem1 Or &H8
Else
Intem1 = Intem1 And &HF7
End If
Case 6
If (Intem1 And &H20) = &H20 Then
Intem1 = Intem1 Or &H10
Else
Intem1 = Intem1 And &HEF
End If
Case 7
If (Intem1 And &H40) = &H40 Then
Intem1 = Intem1 Or &H20
Else
Intem1 = Intem1 And &HDF
End If
Case 8
If (Intem1 And &H80) = &H80 Then
Intem1 = Intem1 Or &H40
Else
Intem1 = Intem1 And &HBF
End If
If Intem = &H1 Then '移完第八位后,如果intem是1(即第一位是1)
Intem1 = Intem1 Or &H80 '则将intem1的第八位置1
Else
Intem1 = Intem1 And &H7F '反之置0
End If
End Select
Next y
Next x
ByteRightMOve = Intem1 '将intem1的值返回给函数名
End Function
'功能: 将字节数据的某一位置 1 或置 0
'入口参数: ByteData 数据
' BitNumber 位号
' BitHandleType 位操作类型 0 置0 , 1 置1
'返回值: 处理后的字节数据
Public Function BitHandle(ByVal BitHandleType As Long, ByVal ByteData As Byte, ByVal BitNumber As Long) As Byte
Select Case BitHandleType
'置0
Case 0
Select Case BitNumber
'1位
Case 1
BitHandle = ByteData And &HFE
'2位
Case 2
BitHandle = ByteData And &HFD
'3位
Case 3
BitHandle = ByteData And &HFB
'4位
Case 4
BitHandle = ByteData And &HF7
'5位
Case 5
BitHandle = ByteData And &HEF
'6位
Case 6
BitHandle = ByteData And &HDF
'7位
Case 7
BitHandle = ByteData And &HBF
'8位
Case 8
BitHandle = ByteData And &H7F
End Select
'置1
Case 1
Select Case BitNumber
'1位
Case 1
BitHandle = ByteData Or &H1
'2位
Case 2
BitHandle = ByteData Or &H2
'3位
Case 3
BitHandle = ByteData Or &H4
'4位
Case 4
BitHandle = ByteData Or &H8
'5位
Case 5
BitHandle = ByteData Or &H10
'6位
Case 6
BitHandle = ByteData Or &H20
'7位
Case 7
BitHandle = ByteData Or &H40
'8位
Case 8
BitHandle = ByteData Or &H80
End Select
End Select
End Function
'功能: 判断字节数据的某一位是 0 还是 1
'入口参数: ByteData 数据
' BitNumber位号
'返回值: 检测位的数据
Public Function CheckBit(ByVal ByteData As Byte, BitNumber As Long) As Byte
Select Case BitNumber
'1位
Case 1
If (ByteData And &H1) = &H1 Then
CheckBit = 1
Else
CheckBit = 0
End If
'2位
Case 2
If (ByteData And &H2) = &H2 Then
CheckBit = 1
Else
CheckBit = 0
End If
'3位
Case 3
If (ByteData And &H4) = &H4 Then
CheckBit = 1
Else
CheckBit = 0
End If
'4位
Case 4
If (ByteData And &H8) = &H8 Then
CheckBit = 1
Else
CheckBit = 0
End If
'5位
Case 5
If (ByteData And &H10) = &H10 Then
CheckBit = 1
Else
CheckBit = 0
End If
'6位
Case 6
If (ByteData And &H20) = &H20 Then
CheckBit = 1
Else
CheckBit = 0
End If
'7位
Case 7
If (ByteData And &H40) = &H40 Then
CheckBit = 1
Else
CheckBit = 0
End If
'8位
Case 8
If (ByteData And &H80) = &H80 Then
CheckBit = 1
Else
CheckBit = 0
End If
End Select
End Function