How to extract numbers from an bitwise OR number in an Array? I have an array of numbers, say [1,2,3,4,5,6], I want to find the element in the array which is equal to the bitwise OR of remaining elements.
Currently, my approach is to find bitwise OR of all the numbers in the array as follows, i.e., $$X = 1 | 2|3|4|5|6$$ Then I am trying to just filter out one a number at a time from X and check if they are equal. For example, if I am checking for number 4, I want something like this - $$(\{X-4\} == 4)?$$ I can easily do this in case of XOR. But not sure how to achieve $\{X-4\}$ in case of bitwise OR?
 A: You are making things far too hard. Rather than successively taking out one element at a time, and doing a check, you can simply take a one-time bitwise OR of all elements: If there is an element that is the result of the bitwise OR on all others, then that element will also be the bitwise OR of all elements, including itself!
Here is why:
Assuming that there is indeed one such element, then for any bit position it will be true that the bit of the element you are looking for is a $1$ if and only if at least one of the bits of the other elements is a $1$. 
So, it is impossible to have that bit being a $1$ when the corresponding bits of the other numbers are all $0$, and it is also impossible for that bit being a $0$ when there is at least one corresponding bit that's a $1$. 
That means that if we OR the value of the bit of the element we are looking for with all the corresponding bits from all the other elements, we get the value of the first bit. 
Therefore, the value of that bit equals the value of the OR of all the bits for that position, and thus we can simply take the OR of all bits for that position to find the value of the bit we want.
In other words, you can simply take the bitwise OR for all elements, and then see which of the elements equals the result of that: that's the element you are looking for.
So notice that with numbers $1$ through $6$ there is no element that is the result of the bitwise OR on all other elements, because for every position there are at least two $1$'s, and hence the bitwise or of any fice elements will give you $111$, i.e. $7$.
However, if you take numbers $1$ through $7$, then not only is $7$ the bitwise OR of $1$ through $6$, but also of $1$ through $7$, and as I just explained, it has to be that way, again assuming there is such an element in the first place.
