7,764
社区成员
发帖
与我相关
我的任务
分享
Dim int_temp As Integer
Dim int_temp2 As Integer
int_temp = int(Text1.Text)
int_temp2 = int_temp And &H8000
int_temp = Not int_temp
int_temp = int_temp And &H7FFF
int_temp = int_temp + 1
If int_temp2 <> 0 Then
int_temp = int_temp Xor &H8000
End If
Text2.Text = int_temp
Option Explicit
Private Sub Command1_Click()
Dim x As Integer, y As Integer
x = CInt(Text1.Text)
Debug.Print "原码:", x, Right("0000" & Hex(x), 4), DEC2Bin(x)
'反码:除符号位以外,各位取反
y = (x And &H8000) Or (Not x)
Debug.Print "反码:", y, Right("0000" & Hex(y), 4), DEC2Bin(y)
'补码:除符号位以外,各位取反,末位加一
y = (x And &H8000) Or ((Not x) + 1)
Debug.Print "补码:", y, Right("0000" & Hex(y), 4), DEC2Bin(y)
'移码对补码符号位取反
y = (Not (x And &H8000)) And ((Not x) + 1)
Debug.Print "移码:", y, Right("0000" & Hex(y), 4), DEC2Bin(y)
Debug.Print
End Sub
Private Function DEC2Bin(intX As Integer) As String
Dim strHex As String
Dim i As Integer
strHex = Right("0000" & Hex(intX), 4)
For i = 1 To 4
Select Case Mid(strHex, i, 1)
Case "0"
DEC2Bin = DEC2Bin & "0000"
Case "1"
DEC2Bin = DEC2Bin & "0001"
Case "2"
DEC2Bin = DEC2Bin & "0010"
Case "3"
DEC2Bin = DEC2Bin & "0011"
Case "4"
DEC2Bin = DEC2Bin & "0100"
Case "5"
DEC2Bin = DEC2Bin & "0101"
Case "6"
DEC2Bin = DEC2Bin & "0110"
Case "7"
DEC2Bin = DEC2Bin & "0111"
Case "8"
DEC2Bin = DEC2Bin & "1000"
Case "9"
DEC2Bin = DEC2Bin & "1001"
Case "A"
DEC2Bin = DEC2Bin & "1010"
Case "B"
DEC2Bin = DEC2Bin & "1011"
Case "C"
DEC2Bin = DEC2Bin & "1100"
Case "D"
DEC2Bin = DEC2Bin & "1101"
Case "E"
DEC2Bin = DEC2Bin & "1110"
Case "F"
DEC2Bin = DEC2Bin & "1111"
End Select
Next
End Function
两个案例输出:
原码: 123 007B 0000000001111011
反码: -124 FF84 1111111110000100
补码: -123 FF85 1111111110000101
移码: -123 FF85 1111111110000101
原码: -123 FF85 1111111110000101
反码: -32646 807A 1000000001111010
补码: -32645 807B 1000000001111011
移码: 123 007B 0000000001111011