Sometimes you need to do many logical operations at once. For example, consider the problem where you want to find which numbers exist in both of two strings, which are stored in an 2 cells.
A1:
12345
A2:
246
The answer is 24, but how would you do it? You could do something with Search but it would be horrendously complicated. Imagine also if the list was not even sorted,
A1:
32451
A2:
426
Using Search would become even more complex. Using the bit twiddling formulas we will look at here, the formula to solve both problems is the same,
=bitAnd(a1:a2)
You can even sort the list , and remove duplicates, by using:
You can also test if a a list is a subset of another list
bitAnd(a1).
if ( bitand(a1,a2)=a1, “subset”,”not subset”)
if ( bitand(a1,a2)=bitand(a1), “subset”,”not subset”) ‘ this would be a better test, as it would ensure that a1 was sorted in the right order before comparing them.
There are a few UDFs associated with this capability, but for the moment we will focus on the AND function.
bitAND(a[,b])
=bitAnd(A65) ‘ Sorts and deduplicates the contents of the list in A65
=bitAND(A20,A21) ‘ ANDS the list in a20 with the one in A21
{=bitAND(“12”,A2:A4)} ‘ entered as an array formula, will and 12 with each of a2:a4 and return an array with 3 results
{=afsep(“”,IF(bitAnd(C64:C72,E66)=C64:C72,B64:B72,””))} ‘ if any of c64:C72 is a subset of C64, show the corresponding value in B64:B72, and use the afsep function to display the resulting array.
Subpages
bruce mcpherson is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.mcpher.com. Permissions beyond the scope of this license may be available at code use guidelines