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