How to perform BITWISE OR on numbers?

https://www.codechef.com/problems/CHNGOR

see you are given a list of integers (1,2,3,4,5,6,7,8,9) and you need remove all elements from the list by choosing subsequence in such a way that if you find OR of the all elements of the subsequence and then add all the OR of every subsequence it must be the minimum.
Example:- 1st subsequence 1,2,3 OR=3
2nd subsequence 4,5,6 OR=7
3rd subsequence 7,8,9 OR=15
total sum=3+7+15=25
now if we take 1st subsequence 1,2,3,4 OR=7
2nd subsequence 5,6 OR=7
subsequence 7,8,9 OR=15
total sum=7+7+15=29
so the minimum cost is 25 here (just an example,this is not the actual answer) there are more subsequence just find one with minimum sum.

1 Like

Thanks a lot

If I take the whole array as one sub-sequence then it will give correct ans but for getting correct ans will i have to convert each no into binary than do or operation then convert it to number or there is another way ? (please tell if any)

If you are using any bitwise operation, you have to do it on numbers and NOT on their binary representation.

Example:
(1 | 2) \rightarrow 3

And if you converted this into binary form,
(1 | 10) \rightarrow 11

The answer you would want is 3, so bitwise operations on numbers yield the desired answer.

1 Like

how to do bitwise operation on numbers?

int a = 10, b = 20;
//bitwise operators
int AandB = a&b;
int AorB = a|b;
int AxorB = a^b;

2 Likes

so if write 2|3 it will give ans for bitwise 2 or 3 ??
will it also give answer for multiple elements
2|3|4|5|1|22|33

Yes.

1 Like

Ok thanks a lot for help !
If you use C can you tell in which library I will get this ( | ) operation

It is built in operator. Just like + - / * .

2 Likes

Ok sir thanks a lot !

1 Like