 # Bit_mask & Constructive algorithm

Problem

can anyone explain our approach to this problem??

I used the following approach-
for every bit position [0,10), check the value in the final answer if starting bit is 0 or 1
cases-

1. both 1 which means or with is the deciding factor (let us say we have 11 and 100 as our binary numbers, in order to convert 11->11 and 100->101 we do bitwise or with 1)
2. both 0 which means and with 0 is the deciding factor
3. the bit that was 1 is 0 and the bit that was 0 is 1, it means xor with 1 is the deciding factor
4. the bits don’t change, now this is only possible when we do and with 1

So using these, we can find the values with which we can-

1. xor: just set the bits where we need to flip the values
2. and: first set all bits and then unset the ones where and 0 (pt 2) is the deciding factor
3. or: set all bits where answer is 1

now print in following order-
xor
and (can interchange these 2)
or
We are using 3 moves only.

1 Like

didn’t get the last three points

``````
1.  xor: just set the bits where we need to flip the values
2.   and: first set all bits and then unset the ones where and 0 (pt 2) is the deciding factor
3.     or: set all bits where answer is 1

``````

at any index, the bit value will either be 0 or 1 in the starting right?
now finally, bit value can be 00,11,01 or 10
now our original combination is 01(for any bit) and final combination is any 1 of the 4.
01->00 the value has been & with 0 (0&0=0, 0&1=0)
01->01 no change, value has been & with 1
01->11, both ones, definitely brough by bitwise or | with 1
01->10, xor with 1 has been done
u just need to see which operation has been done on which bit