我想在不使用Visual Basic .NET中的内置函数的情况下使用Xor
Xor description:
它是逻辑和按位逻辑异或运算符。
如果两个表达式都为True或两个表达式都为False,则返回True;否则返回False。
此运算符不执行短路,它总是计算两个表达式,并且没有此运算符的短路对应
这甚至可能吗?如果是这样,怎么样?
答案 0 :(得分:1)
XOR只是一个逻辑操作。如果你愿意,你可以完全用NAND代替它,因为NAND在功能上是完整的。
XOR可能是
(a and not b) or (not a and b)
或
(a or b) and (not a or not b)
或者如果你根本不想要逻辑运算符......
If(a) Then
If(not b) Then
Return True
End If
Else If(not a) Then
If(b) Then
Return True
End If
End If
Return False
所以基本上,是的 - 我想有很多方法可以做到这一点。但我想不出你为什么会这么想。
答案 1 :(得分:0)
我认为这个定义不正确。异或表示只有一个表达式为真。该定义是Exclusive NOR。
但无论如何
XOR:
(bool1 and not bool2) or (not bool1 and bool2)
XNOR:
(bool1 and bool2) or (not bool1 and not bool2)
甚至只是
bool1 = bool2
答案 2 :(得分:0)
正如其他人所指出的那样,XOR所述的定义是不正确的。 XOR只是无需携带即可添加。
Dim b1 As Boolean = False
Dim b2 As Boolean = False
For x As Integer = 1 To 2
For y As Integer = 1 To 2
Dim n1 As Integer = CInt(b1)
Dim n2 As Integer = CInt(b2)
Dim ans As Boolean = CBool((n1 + n2) And 1) 'add, ignore carries
Debug.WriteLine("b1 {0} b2 {1} ans {2}", b1, b2, ans)
b2 = Not b2
Next
b1 = Not b1
Next
答案 3 :(得分:0)
Dim val1, val2 As Boolean
If Val1 <> Val2 Return False
Else Return True
End If
'那是XNOR