Given two binary numbers with N bits (for the example I'll use 5)
- XXX0X This means that 1) has all combinations of binary numbers where the 2nd bit is 0, so a total of 16 combinations ( X is both 0 and 1)
- XXXX0 This one instead is all the binary combinations of binary numbers where the 1st bit is 0, so here too 16 combinations.
Now the issue is that those 2 share some combinations, so if i wanna take out from all 32 possible numbers that can be written with 5 bits, only the numbers that are part of those 2 combinations i will do:
- Union 2)= 1) + 2) - 1) Intersecate 2) = 16+16-8= 24 combinations
Is there any algorithm to do this union and intersection without doing something like creating all possible combinations and then check witch ones are equal?
I thought about the simpliest thing of creating all 2^n possible binary numbers with N bits and then confronting strings to check for the intersection but i guess there should be a faster way of doing it. I didn't even know what to search if it was a known problem or something like that, so if it already exists or it's known I'm sorry.
edit:
To give more about my issue: i'm working on 3-SAT, so i have a code to solve by dpll algorithm a given problem with n variables and m clauses. when the code encounters a conflict it backtracks and gives me the reason of the conflict that's an array of k values that represents the combination of assegnation of variables that led to the conflict.
For example if i have 5 variables and the reason for the first conflict is [2,-5], i know that all combinations that have both the variable x2 as true and the variable x5 as false are out, for this example, 16 combinations are out of the possible 32.
Now if the next reason of the conflict is for example [-1,3], i know that this leaves out 16 combinations, but some are the same of the first reason, so , to know at any time how many i have already left out of the whole 2^n i have to know how many combinations the 2 reasons [2,-5][-1,3] eliminate.
i tought about creating a data structure with all 2^n combinations and every time i get the reason i pop the combinations i don't need anymore so that i won't have to check for intersections and unions.
My only issue is that i work with 100/200 variables so to create a structure with 2^200 variables seems unreasonable.
It's easier to check for set bits (=1) rather than zeroes. You can always negate (
~) your patterns if you need zeroes.Now, if you have two test patterns:
you can
orthem togetherand then, for each number
iFull code: