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
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.
=1-(A1<>0)+(A2<>0) for each bit.
You can split it into individual columns for the above formula using this: =MID(A1|7|1) =MID(A1|6|1) =MID(A1|5|1) =MID(A1|4|1) =MID(A1|3|1) =MID(A1|2|1) =MID(A1|1|1) ...
' this VBA returns a double that has to be formatted on the worksheet.
Option Explicit
Public Function MYXOR(r1 As Range, r2 As Range) As Double
'r1 and r2 are expected as HEX; for example,
'DEC2HEX(CODE("B")) returns ASCII of "B" as HEX
On Error GoTo ErrHandler
MYXOR = "&H" & r1.Value Xor "&H" & r2.Value
GoTo CleanUp
ErrHandler:
MYXOR = Err.Number
Resume CleanUp
CleanUp:
' format the double being returned in MYXOR with TEXT(DEC2HEX(MYXOR(C9,F9)),"00000")
' number of leading zeroes according to the size of the HEX in r1 and r2
End Function
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.