2

I have the following C statement:

int res = x & (x ^ y);

Is there a way to do the same thing, but using x and y only one time each?

For example:

x | (~x & y) == x | y

2 Answers 2

7

Yes, by expanding the xor (a ^ b == (a & ~b) | (~a & b)), and then simplifying the result, one gets:

res = x & ~y;
Sign up to request clarification or add additional context in comments.

Comments

4

x & (x ^ y) sets the bits that are set on x and set on x^y e.g. not set on y.

So you can do:

int res = x & ~y;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.