Vb.net 如何在VB中使用Xor而不使用内置Xor?

Vb.net 如何在VB中使用Xor而不使用内置Xor?,vb.net,function,xor,Vb.net,Function,Xor,我想使用Xor而不使用Visual Basic.NET中的内置Xor 异或描述: 它是逻辑以及位逻辑异或运算符 如果两个表达式都为True或都为False,则返回True;否则返回False 此运算符不执行短路,它始终计算两个表达式,并且没有此运算符的短路对应项 这可能吗?如果是这样,怎么做?我认为这个定义是不正确的。Exclusive OR表示一个且只有一个表达式是真的。这一定义是排他性的,也不是强制性的 但无论如何 XOR: (bool1 and not bool2) or (not boo

我想使用Xor而不使用Visual Basic.NET中的内置Xor

异或描述:

它是逻辑以及位逻辑异或运算符

如果两个表达式都为True或都为False,则返回True;否则返回False

此运算符不执行短路,它始终计算两个表达式,并且没有此运算符的短路对应项


这可能吗?如果是这样,怎么做?

我认为这个定义是不正确的。Exclusive OR表示一个且只有一个表达式是真的。这一定义是排他性的,也不是强制性的

但无论如何

XOR:
(bool1 and not bool2) or (not bool1 and bool2)

XNOR:
(bool1 and bool2) or (not bool1 and not bool2)
或者干脆

bool1 = bool2

XOR只是一个逻辑运算。如果需要,可以完全用NAND替换它,因为NAND在功能上是完整的

XOR可能是

(a and not b) or (not a and 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

所以基本上,是的,我想有很多方法可以做到这一点。但是我想不出你为什么要这么做。

对于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

Dim val1,val2为布尔值
如果Val1 Val2返回False
Else返回True
End If


“这就是XNOR”

内置的有什么问题吗?我看不出这怎么可能。如果不计算两个表达式的值,它如何测试两个表达式的值是否相同?我只想知道是否可以不使用内置表达式。您在“Xor”描述下描述的是Xor的否定。如果为1,则Xor返回true,并且只有1个表达式为true。
    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