EXCEL XOR multiple bits

James Van Boxtel picture James Van Boxtel · Feb 9, 2009 · Viewed 37k times · Source

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.

Answer

Robin Day picture Robin Day · Feb 9, 2009

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)))