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.
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).
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).
= 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.
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.
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)))
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.
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:
BitPositions
as ={1,2,3,4,5,6,7}
(7-bit),BitStrings
as ={1000000,100000,10000,1000,100,10,1}
(7-bit),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.:
BitPositions
as =ROW(INDIRECT("1:32"))
(32-bit),BitStrings
as =10^(32-ROW(INDIRECT("1:32")))
(32-bit),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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With