Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXCEL XOR multiple bits

Tags:

excel

xor

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 boolean values

=OR(AND(A1,NOT(A2)),AND(A2,NOT(A1)))

but it doesn't work for a string of bits.

like image 890
James Van Boxtel Avatar asked Feb 09 '09 18:02

James Van Boxtel


People also ask

How do you use Bitwise XOR in Excel?

To use the XOR function, simply type =XOR and Excel will prompt you to enter logical statements. You can also just feed it an array of true/false values. For example in the video, the reference range that we want is (A2=A$2:A2).

How can I get XOR 2 numbers in Excel?

The Excel XOR formula is =XOR(A1,A2). A similar formula to XOR in Excel is the Excel OR formula, which is =OR(A1,A2). The Excel AND formula is =AND(A1,A2).

How do I create an XOR formula in Excel?

= XOR(logical1, [logical2],…) The XOR function includes the following arguments: Logical1, logical2 – Logical1 is a required argument, whereas logical2 and subsequent logical values are optional.

What is the difference between OR and XOR in Excel?

Whereas the OR function returns true if any input is TRUE, XOR only returns TRUE in specific cases. In the simplest case, with just two logical statements, XOR returns TRUE only if one of the logicals is TRUE. If both values are TRUE, XOR returns FALSE.


3 Answers

You need to use VBA to do this. If you open VBA, create a new Module and enter the function

Public Function BITXOR(x As Long, y As Long)
    BITXOR = x Xor y
End Function

You can then use the DEC2BIN and BIN2DEC to convert from binary to decimal to run this function. For example:

Cell A1 = 0111010

Cell A2 = 0101011

=DEC2BIN(BITXOR(BIN2DEC(A1),BIN2DEC(A2)))
like image 145
Robin Day Avatar answered Oct 22 '22 00:10

Robin Day


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.

like image 37
Patrick Cuff Avatar answered Oct 22 '22 01:10

Patrick Cuff


Here is a solution without using VBA:
=TEXT(SUMPRODUCT(MOD(INT(MID(A1,{1,2,3,4,5,6,7},1))+INT(MID(A2,{1,2,3,4,5,6,7},1)),2),{1000000,100000,10000,1000,100,10,1}),"0000000")

This calculates the bitwise XOR using SUMPRODUCT and TEXT to turn it into a string of bits.

Note: this formula requires both input values to have length 7 (as per your own example) and the output will also have length 7. To allow for different input lengths, simply implement the necessary truncation and/or padding.


You can choose to use some shorthand definitions:

  • define BitPositions as ={1,2,3,4,5,6,7} (7-bit),
  • define BitStrings as ={1000000,100000,10000,1000,100,10,1} (7-bit),
  • define BitFormat as ="0000000" (7-bit),

then your formula can be made a bit more legible/shorter/cleaner:
=TEXT(SUMPRODUCT(MOD(INT(MID(A1,BitPositions,1))+INT(MID(A2,BitPositions,1)),2),BitStrings),BitFormat)

This also makes it easier to work with larger strings of bits, e.g.:

  • define BitPositions as =ROW(INDIRECT("1:32")) (32-bit),
  • define BitStrings as =10^(32-ROW(INDIRECT("1:32"))) (32-bit),
  • define BitFormat as =REPT("0",32) (32-bit)

Should you wish to implement NOT/OR/AND/etc. then you can get your inspiration from these formulas for the decimal counterparts; here are some more in-depth explanations for XOR with SUMPRODUCT though it also uses decimal inputs.

like image 45
mousio Avatar answered Oct 22 '22 02:10

mousio