Exclusive or between N bit sets

I am implementing a Java program using BitSets, and I am stuck in the following operation:

Given that N BitSets returns a BitSet with 0 if there is more than 1 in all bit sets, otherwise

As an example, suppose we have these 3 sets:

  • 10010
  • 01011
  • 00111

  • 11100 expected result

For the following sets:

  • 10010
  • 01011
  • 00111
  • 10100
  • 00101

  • Expected Result 01000

I try to make it exclusive with a little wise operations, and I realized that what I need is literally exclusive or between all sets, but not in iterative mode, so I'm completely confused about what to do. Is it possible?

I wanted to avoid an expensive solution, to check every bit in every set and keep a counter for every position ...

Thanks for any help

: , , . -, , 1 1 , , , 1 ,

+5
2

, , . , , - , . .

int[] ints = {0b10010, 0b01011, 0b00111, 0b10100, 0b00101};
int setOnce = 0, setMore = 0;
for (int i : ints) {
    setMore |= setOnce & i;
    setOnce |= i;
}
int result = setOnce & ~setMore;
System.out.println(String.format("%5s", Integer.toBinaryString(result)).replace(' ', '0'));

01000
+4

, , , . - , , (.. , ).

XOR , , . XOR , . , , ( ).

+1

All Articles