vb.net - 十六进制比较 (If ((&H7F And &H80) = &H80)) 意味着什么?

标签 vb.net compare hex

我从免费来源获得此代码

  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/

相关文章:

vb.net - 是否可以在子类中分配一个 ReadOnly 变量?

python - 检查是否有与输入图像完全相同的图像

php - 我的 IF 函数不工作

hex - Python-sns.color_palette输出为Bokeh的十六进制数字

vb.net - 如何在vb代码内的DataTable上的linq中正确执行 "group by"?

vb.net - 如何在没有 Linq 的情况下查询 DataGridView

sql-server - SQL 参数值被截断

r - 比较两列并更改第三列时如何使用 ifelse?

encryption - 使用 MS seal 比较两个密文?

java - 十六进制字符串到 int 的转换无法正常工作