vb怎样调换2进制数的高低位?如: 0000 1010(bin) -> 0101 0000(bin) 、 1110 0000(bin) -> 0000 0111
2个回答
展开全部
实际上是1个2进制数连续右移移出
其结果连续左移移入
Dim i As Integer
Dim a As Byte
Dim b As Byte
a = 10 '10的二进制=00001010
b = 0
For i = 1 To 8
b = b + (a Mod 2) * 2 ^ (8 - i) '取a的2进制最后1位放到结果的 87654321对应位
a = a / 2 '待处理数右移1位
Next i
Print b '结果b=80 '二进制=01010000
其结果连续左移移入
Dim i As Integer
Dim a As Byte
Dim b As Byte
a = 10 '10的二进制=00001010
b = 0
For i = 1 To 8
b = b + (a Mod 2) * 2 ^ (8 - i) '取a的2进制最后1位放到结果的 87654321对应位
a = a / 2 '待处理数右移1位
Next i
Print b '结果b=80 '二进制=01010000
追问
亲,如果a=010AE02A43这个字符串,需要改动的地方是哪里?
追答
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim txt As String
Dim a(10) As Byte
Dim b(10) As Byte
txt = "010AE02A43"
k = Len(txt) / 2
For i = 1 To k
a(i) = "&h" & Mid(txt, (i - 1) * 2 + 1, 2)
Next i
For j = 1 To k
For i = 1 To 8
b(j) = b(j) + (a(j) Mod 2) * 2 ^ (8 - i) '取a的2进制最后1位放到结果的 87654321对应位
a(j) = Int(a(j) / 2) '待处理数右移1位
Next i
Next j
txt = ""
For i = k To 1 Step -1
txt = txt & Hex(b(i))
Next i
Print txt
展开全部
Function GetNotBit(inBit As Byte) As Byte
Static isload As Boolean
Static b(7) As Byte
Dim i As Integer
If isload = False Then
b(0) = 1
For i = 1 To 7
b(i) = b(i - 1) + b(i - 1)
Next
isload = True
End If
For i = 0 To 7
If inBit And b(i) Then
GetNotBit = GetNotBit Or b(7 - i)
Else
GetNotBit = GetNotBit And Not (b(7 - i))
End If
Next
End Function
调用 Me.Caption = GetNotBit(14)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询