簡體   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