这个十六进制比较 (If ((&H7F And &H80) = &H80)) 是什么意思?
What does this comparison of hexadecimal (If ((&H7F And &H80) = &H80)) means ?
我从免费来源获得此代码
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 将始终 return 相同的值
例如
0111111 AND (&H7F)
0100000 = (&H40)
-------
0100000 (&H40)
您可以在此处BitWise operators in VB.NET and C#找到更详细的文章
要启用所有字段,您需要将 FieldByte 变量设置为 &HFF,因此
1111111 AND (&HFF)
1000000 = (&H80)
------------
1000000 (&H80)
或者你可以实现一个带有标志属性的枚举来获得更少的神秘代码:
Module Demo
Dim FieldTime As Boolean
Dim FieldOpenInterest As Boolean
Dim FieldOpenPrice As Boolean
Dim FieldHigh As Boolean
Dim FieldLow As Boolean
Sub Main()
'SetFields(&HFF) ' would set all flags as mentioned in the other answer,
' however all of them not only the ones specified
SetFields(FieldInfo.FieldTime Or _
FieldInfo.FieldOpenInterest Or _
FieldInfo.FieldOpenPrice Or _
FieldInfo.FieldOpenPrice Or _
FieldInfo.FieldHigh Or _
FieldInfo.FieldLow) 'same thing but less cryptic and you dont have to think about what you do so much
End Sub
Private Sub SetFields(ByRef FieldByte As FieldInfo)
FieldTime = (FieldByte And FieldInfo.FieldTime) = FieldInfo.FieldTime
FieldOpenInterest = (FieldByte And FieldInfo.FieldOpenInterest) = FieldInfo.FieldOpenInterest
FieldOpenPrice = (FieldByte And FieldInfo.FieldOpenPrice) = FieldInfo.FieldOpenPrice
FieldHigh = (FieldByte And FieldInfo.FieldHigh) = FieldInfo.FieldHigh
FieldLow = (FieldByte And FieldInfo.FieldLow) = FieldInfo.FieldLow
End Sub
<Flags()>
Private Enum FieldInfo ' hope i got the calculations right =P
FieldHigh = 16
FieldTime = 128
FieldOpenPrice = 32
FieldOpenInterest = 64
FieldLow = 8
End Enum
End Module
我从免费来源获得此代码
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 将始终 return 相同的值
例如
0111111 AND (&H7F)
0100000 = (&H40)
-------
0100000 (&H40)
您可以在此处BitWise operators in VB.NET and C#找到更详细的文章
要启用所有字段,您需要将 FieldByte 变量设置为 &HFF,因此
1111111 AND (&HFF)
1000000 = (&H80)
------------
1000000 (&H80)
或者你可以实现一个带有标志属性的枚举来获得更少的神秘代码:
Module Demo
Dim FieldTime As Boolean
Dim FieldOpenInterest As Boolean
Dim FieldOpenPrice As Boolean
Dim FieldHigh As Boolean
Dim FieldLow As Boolean
Sub Main()
'SetFields(&HFF) ' would set all flags as mentioned in the other answer,
' however all of them not only the ones specified
SetFields(FieldInfo.FieldTime Or _
FieldInfo.FieldOpenInterest Or _
FieldInfo.FieldOpenPrice Or _
FieldInfo.FieldOpenPrice Or _
FieldInfo.FieldHigh Or _
FieldInfo.FieldLow) 'same thing but less cryptic and you dont have to think about what you do so much
End Sub
Private Sub SetFields(ByRef FieldByte As FieldInfo)
FieldTime = (FieldByte And FieldInfo.FieldTime) = FieldInfo.FieldTime
FieldOpenInterest = (FieldByte And FieldInfo.FieldOpenInterest) = FieldInfo.FieldOpenInterest
FieldOpenPrice = (FieldByte And FieldInfo.FieldOpenPrice) = FieldInfo.FieldOpenPrice
FieldHigh = (FieldByte And FieldInfo.FieldHigh) = FieldInfo.FieldHigh
FieldLow = (FieldByte And FieldInfo.FieldLow) = FieldInfo.FieldLow
End Sub
<Flags()>
Private Enum FieldInfo ' hope i got the calculations right =P
FieldHigh = 16
FieldTime = 128
FieldOpenPrice = 32
FieldOpenInterest = 64
FieldLow = 8
End Enum
End Module