我不懂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 = &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
使用以下功能时
'If (ahora and Turnos.DESAYUNO) <> 0 then returns Turnos.DESAYUNO
我的问题是为什么(ahora和Turnos.DESAYUNO))&lt;&gt; 0 ??和下一个功能
'If (ahora And Turnos.COMIDA) = 0'
对不起,我不明白哪个是逻辑。有人能帮助我吗?
答案 0 :(得分:1)
And
此处充当bitwise and
我认为一个更简单的例子可能会帮助你理解它:
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
答案 1 :(得分:1)