Home > Back-end >  asymmetric difference betwen two binary numbers (bitsets)
asymmetric difference betwen two binary numbers (bitsets)

Time:09-18

It is quick and easy to determine shared/different bits between two binary numbers by AND or XOR. Let's say we have A: 10011 and B:11001 we can get the difference.

10011 XOR 11001 = 01010 (1s are different 0 similar.)

Are there any quick and easy logic or arithmetic operations that could produce similar but asymmetric output (1s showing these that are for example present in A but missing in B or vice vs.)

Example 10011 ??? 11001 = 00010 (1s mean present in left hand operand missing in right)

Could it be done with some quick arithmetic/logic or would I have to start some loop to go through the comparisons one by one?

I got to this question when I was contemplating on storing some presence/absence data in bytes as bit flags (for memory efficiency) -- and were already gleeful in the fact that were I do thusly I could then do quick and easy data diffing operations, but for many applications the direction of difference is also important.

CodePudding user response:

The more canonical way to express this is A AND (NOT B), where NOT flips all bits.

CodePudding user response:

tkausl - comment on the question answers it successfully. (A XOR B) AND A would really do the trick. XOR would generate the difference between A and B and then AND mask with A to show only these that are present in A. Result difference showing these bits that are set in A but not in B.

  • Related