Vb.net 这些带有条件逻辑的If语句的目的是什么?
我不理解VB的这种逻辑。我见过一个常数的例子:Vb.net 这些带有条件逻辑的If语句的目的是什么?,vb.net,Vb.net,我不理解VB的这种逻辑。我见过一个常数的例子: Enum Turnos Ninguno = 0 'nothing the constant is = 0 Desayuno = &H380 'breakfast the constant is = 896 comida = &H1E000 'lunch the constant is = 122888 Cena = &H
Enum Turnos
Ninguno = 0 'nothing the constant is = 0
Desayuno = &H380 'breakfast the constant is = 896
comida = &H1E000 'lunch the constant is = 122888
Cena = &HE00003 'dinner the constant is = 14680067
end Enum
Sub Main()
Console.WriteLine("Es la hora {0:hh:mm:ss tt}", DateTime.Now)
Console.WriteLine("Turno: {0:G}", QuéTurnoEsAhora())
Console.ReadKey()
End Sub
Public Function QuéTurnoEsAhora() As Turnos
Dim ahora As Integer = CInt(Math.Pow(2, DateTime.Now.Hour))
If (ahora And Turnos.DESAYUNO) <> 0 Then Return Turnos.DESAYUNO
If (ahora And Turnos.COMIDA) <> 0 Then Return Turnos.COMIDA
If (ahora And Turnos.CENA) <> 0 Then Return Turnos.CENA
Return Turnos.NINGUNO
End Function
对不起,我不明白这是什么逻辑。有人能帮帮我吗?这里的
和充当了一个角色。
我认为一个简单的例子可能有助于您理解它:
Enum bitwiseExample
Empty = 0 ' 0000
One = 1 ' 0001
Two = 2 ' 0010
Four = 4 ' 0100
Eight = 8 ' 1000
EndEnum
Dim x as integer = 6 ' 0110
x and bitwiseExample.Empty = 0 ' since 0110 & 0000 = 0000
x and bitwiseExample.One = 0 ' since 0110 & 0001 = 0000
x and bitwiseExample.Two = 2 ' since 0110 & 0010 = 0010
x and bitwiseExample.Four = 4 ' since 0110 & 0100 = 0100
枚举中的每个常量都是一个位掩码,它与一天中的特定时间重叠,具体取决于应该吃的食物
例如,十六进制值为380,十进制值为896的早餐,其二进制值为(24位)00000000000000 1110000000。从最低有效位开始计数并从零开始,第7位、第8位和第9位为高位。如另一个答案所述,按位and用于用该值屏蔽当前小时。仅当当前小时数等于7、8或9时,结果才为1
其他的一餐也是如此。您对午餐的评论中有一个错误,应该是十进制122880,而不是122888
这是一张桌子
你为什么要使用Math.Pow
?与其问你不懂的代码教程,不如解释一下你想做什么?你的结构&H..
不正确:)非常感谢Zohar,现在我再次清楚了thansk。
'If (ahora And Turnos.COMIDA) = 0'
Enum bitwiseExample
Empty = 0 ' 0000
One = 1 ' 0001
Two = 2 ' 0010
Four = 4 ' 0100
Eight = 8 ' 1000
EndEnum
Dim x as integer = 6 ' 0110
x and bitwiseExample.Empty = 0 ' since 0110 & 0000 = 0000
x and bitwiseExample.One = 0 ' since 0110 & 0001 = 0000
x and bitwiseExample.Two = 2 ' since 0110 & 0010 = 0010
x and bitwiseExample.Four = 4 ' since 0110 & 0100 = 0100