Module1:
Option ExplicitPrivate Function Int2Bin(ByVal x As Long) As String
Dim strHex As String, strTail As String, strTmp As String, i As Integer
strHex = Right("000" & Hex(x), 4)
For i = 1 To 4
Select Case Val("&H" & Mid(strHex, i, 1))
Case 0
strTail = "0000"
Case 1
strTail = "0001"
Case 2
strTail = "0010"
Case 3
strTail = "0011"
Case 4
strTail = "0100"
Case 5
strTail = "0101"
Case 6
strTail = "0110"
Case 7
strTail = "0111"
Case 8
strTail = "1000"
Case 9
strTail = "1001"
Case 10
strTail = "1010"
Case 11
strTail = "1011"
Case 12
strTail = "1100"
Case 13
strTail = "1101"
Case 14
strTail = "1110"
Case 15
strTail = "1111"
End Select
strTmp = strTmp & strTail
Next i Int2Bin = strTmp
End FunctionPrivate Function Bin2Int(ByVal strBin As String) As Long
Dim strHex As String, strTail As String, strTmp As String, i As Integer
For i = 1 To 16 Step 4
Select Case Mid(strBin, i, 4)
Case "0000"
strTail = "0"
Case "0001"
strTail = "1"
Case "0010"
strTail = "2"
Case "0011"
strTail = "3"
Case "0100"
strTail = "4"
Case "0101"
strTail = "5"
Case "0110"
strTail = "6"
Case "0111"
strTail = "7"
Case "1000"
strTail = "8"
Case "1001"
strTail = "9"
Case "1010"
strTail = "A"
Case "1011"
strTail = "B"
Case "1100"
strTail = "C"
Case "1101"
strTail = "D"
Case "1110"
strTail = "E"
Case "1111"
strTail = "F"
End Select
strTmp = strTmp & strTail
Next i Bin2Int = Val("&H" & strTmp & "&")
End FunctionPublic Function Word_Rotate_Left(ByVal x As Long, ByVal bits As Integer) As Long
Dim strTmp As String strTmp = Int2Bin(x)
strTmp = Right(strTmp, 16 - bits) & Left(strTmp, bits)
Word_Rotate_Left = Bin2Int(strTmp)
End FunctionPublic Function Word_Rotate_Right(ByVal x As Long, ByVal bits As Integer) As Long
Dim strTmp As String, intTmp As Integer strTmp = Int2Bin(x)
strTmp = Right(strTmp, bits) & Left(strTmp, 16 - bits)
Word_Rotate_Right = Bin2Int(strTmp)
End FunctionForm1:
Option ExplicitConst A As Integer = &H9596Private Sub Command1_Click()
MsgBox Word_Rotate_Left(A, 2)
End SubPrivate Sub Command2_Click()
MsgBox Word_Rotate_Right(A, 2)
End Sub
Option ExplicitPrivate Function Int2Bin(ByVal x As Long) As String
Dim strHex As String, strTail As String, strTmp As String, i As Integer
strHex = Right("000" & Hex(x), 4)
For i = 1 To 4
Select Case Val("&H" & Mid(strHex, i, 1))
Case 0
strTail = "0000"
Case 1
strTail = "0001"
Case 2
strTail = "0010"
Case 3
strTail = "0011"
Case 4
strTail = "0100"
Case 5
strTail = "0101"
Case 6
strTail = "0110"
Case 7
strTail = "0111"
Case 8
strTail = "1000"
Case 9
strTail = "1001"
Case 10
strTail = "1010"
Case 11
strTail = "1011"
Case 12
strTail = "1100"
Case 13
strTail = "1101"
Case 14
strTail = "1110"
Case 15
strTail = "1111"
End Select
strTmp = strTmp & strTail
Next i Int2Bin = strTmp
End FunctionPrivate Function Bin2Int(ByVal strBin As String) As Long
Dim strHex As String, strTail As String, strTmp As String, i As Integer
For i = 1 To 16 Step 4
Select Case Mid(strBin, i, 4)
Case "0000"
strTail = "0"
Case "0001"
strTail = "1"
Case "0010"
strTail = "2"
Case "0011"
strTail = "3"
Case "0100"
strTail = "4"
Case "0101"
strTail = "5"
Case "0110"
strTail = "6"
Case "0111"
strTail = "7"
Case "1000"
strTail = "8"
Case "1001"
strTail = "9"
Case "1010"
strTail = "A"
Case "1011"
strTail = "B"
Case "1100"
strTail = "C"
Case "1101"
strTail = "D"
Case "1110"
strTail = "E"
Case "1111"
strTail = "F"
End Select
strTmp = strTmp & strTail
Next i Bin2Int = Val("&H" & strTmp & "&")
End FunctionPublic Function Word_Rotate_Left(ByVal x As Long, ByVal bits As Integer) As Long
Dim strTmp As String strTmp = Int2Bin(x)
strTmp = Right(strTmp, 16 - bits) & Left(strTmp, bits)
Word_Rotate_Left = Bin2Int(strTmp)
End FunctionPublic Function Word_Rotate_Right(ByVal x As Long, ByVal bits As Integer) As Long
Dim strTmp As String, intTmp As Integer strTmp = Int2Bin(x)
strTmp = Right(strTmp, bits) & Left(strTmp, 16 - bits)
Word_Rotate_Right = Bin2Int(strTmp)
End FunctionForm1:
Option ExplicitConst A As Integer = &H9596Private Sub Command1_Click()
MsgBox Word_Rotate_Left(A, 2)
End SubPrivate Sub Command2_Click()
MsgBox Word_Rotate_Right(A, 2)
End Sub
Module1:Option ExplicitPublic Function Integer_Rotate_Left(ByVal x As Long, ByVal bits As Integer) As Long
Dim i As Integer, lngTmp As Long x = x And &HFFFF&
For i = 1 To bits
lngTmp = lngTmp * 2
If x And &H8000& Then lngTmp = lngTmp Or 1
x = x * 2
Next i
Integer_Rotate_Left = (x And &HFFFF&) Or lngTmp
End FunctionPublic Function Integer_Rotate_Right(ByVal x As Long, ByVal bits As Integer) As Long
Dim i As Integer, lngTmp As Long x = x And &HFFFF&
For i = 1 To bits
lngTmp = lngTmp \ 2
If x And 1 Then lngTmp = lngTmp Or &H8000&
x = x \ 2
Next i
Integer_Rotate_Right = x Or lngTmp
End Function
Form1:Option ExplicitConst A As Integer = &H9596Private Sub Command1_Click()
MsgBox Integer_Rotate_Left(A, 2)
End SubPrivate Sub Command2_Click()
MsgBox Integer_Rotate_Right(A, 2)
End Sub
你看到复杂的部分,是 Long 型数字与二进制串之间相互转换的一种方法。当然了,也可以用乘除法配合位与,循环得到这些位。不过,既然如此,与其将这些位放到数组中重组,不如直接获取结果。而且,只需循环要移位的位数,而不需要全部。
int ROR(int v,unsigned char b) {
__asm {
push ecx
mov eax,v
mov cl,b
ror eax,cl
pop ecx
}
}
void main() {
printf("0x%08x\n",ROR(0x12345678,4));
}
//0x81234567