我从免费来源获得此代码
Private Sub SetFields(ByRef FieldByte As Byte)
If ((FieldByte And &H80) = &H80) Then
FieldTime = True
Else
FieldTime = False
End If
If ((FieldByte And &H40) = &H40) Then
FieldOpenInterest = True
Else
FieldOpenInterest = False
End If
If ((FieldByte And &H20) = &H20) Then
FieldOpenPrice = True
Else
FieldOpenPrice = False
End If
If ((FieldByte And &H10) = &H10) Then 'Not sure which is high or low
FieldHigh = True
Else
FieldHigh = False
End If
If ((FieldByte And &H8) = &H8) Then 'Not sure which is high or low
FieldLow = True
Else
FieldLow = False
End If
End Sub
发送的FieldByte值为&H7F,该值表示启用除FieldTime之外的所有字段
FieldByte 的值应该是多少才能启用所有字段?谁能解释一下这种比较意味着什么?
提前致谢
最佳答案
基数 16 值 &H7F 相当于基数 2 中的 01111111。
以 16 为基数的值 &H80 相当于以 2 为基数的 10000000。
所以对于按位运算
0111111 AND (&H7F)
1000000 = (&H80)
------------
0000000
结果为零,您的 FieldTime 设置为 False。
任何其他小于 &H80 的值与按位运算符 AND 结合使用时 &H7F 将始终返回相同的值
例如
0111111 AND (&H7F)
0100000 = (&H40)
-------
0100000 (&H40)
您可以在 BitWise operators in VB.NET and C# 上找到更详细的文章这里
要启用所有字段,您需要将 FieldByte 变量设置为 &HFF
1111111 AND (&HFF)
1000000 = (&H80)
------------
1000000 (&H80)
关于vb.net - 十六进制比较 (If ((&H7F And &H80) = &H80)) 意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29078715/