繁体   English   中英

如何在不使用内置VB的情况下在VB中使用Xor?

[英]How do I use Xor in VB without using the built-in one?

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

Xor描述:

它是逻辑以及按位逻辑“异或”运算符。

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

该运算符不执行短路运算,它始终对两个表达式求值,并且该运算符不存在短路运算符

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

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

因此,基本上,是的-我想您可以通过多种方式来做到这一点。 但我想不出您为什么要这么做。

我认为这个定义是不正确的。 异或表示只有一个表达式为真。 该定义是“异或”。

但无论如何

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

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

甚至只是

bool1 = bool2

正如其他人指出的那样,针对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 As Boolean If Val1 <> Val2 Return False Else Return True End If

'那是XNOR

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM