EXCEL XOR multiple bits

后端 未结 5 2149
[愿得一人]
[愿得一人] 2021-02-20 05:33

Okay I have two cells with a string of bits 0111010 and 0101011. I want to XOR the two together so that the resulting cell would be 0010001.

I know you can use this for

5条回答
  •  情话喂你
    2021-02-20 05:51

    You can do this with VBA:

    Public Function XOR_binary(b1, b2) As String
        Dim len_b1
        Dim len_b2
        Dim len_diff
        Dim i
        Dim bit1
        Dim bit2
    
        ' see if the two string are the same length. If not, add 0's to
        ' the beginning of the shorter string
    
        len_b1 = Len(b1)
        len_b2 = Len(b2)
        len_diff = len_b1 - len_b2
    
        Select Case len_diff
            Case Is < 0
                ' b2 is longer
                b1 = String(Abs(len_diff), "0") & b1
            Case Is = 0
                ' they're the same length
            Case Is > 0
                ' b1 is longer
                b2 = String(len_diff, "0") & b2
        End Select
    
        XOR_binary = ""
    
        For i = Len(b2) To 1 Step -1
            bit1 = CInt(Mid(b1, i, 1))
            bit2 = CInt(Mid(b2, i, 1))
    
            XOR_binary = CInt(bit1 Xor bit2) & XOR_binary
        Next i
    
    End Function
    

    Probably not the best implementation, but it works.

    Using your example, A3 contains:

    =XOR_Binary(A1,A2)
    

    The resulting string will have the same number of bits as the longest string you pass in.

提交回复
热议问题