1

I'm trying to solve a C operation in one step without using a loop.

I have:

unsigned int8 Dummy = 0x3F;//This values are for reference only, so can be different
unsigned int8 Dummy_2 = 0x02;

Dummy = Dummy ^ Dummy_2;

Now Dummy = 0x3D

I must to set the Dummy bits to 0 the bits that are 1 in Dummy_2. My code works ok the first time but if I perform the code two times it toggles back to 1. I can do this with a loop but for the clarity of the code I'm trying to do it in one line.

1
  • 2
    Hint: look at what ~Dummy_2 is.
    – dxiv
    Commented Jun 25, 2020 at 4:27

1 Answer 1

4

I'd negate(*) Dummy_2 and then use that as a mask:

Dummy &= (~Dummy_2);

(*) EDIT:
As the busybee pointed out in the comments, "negate" is an arithmetic term. The proper term here would be "to complement Dummy_2.

2
  • "Negate" is more an arithmetic operation, I would call it "invert" or "complement". Commented Jun 25, 2020 at 5:43
  • @thebusybee Thanks. I had this nagging thought that "negate" was a wrong term, but could find a better one. I've edited my answer with your suggestion.
    – Mureinik
    Commented Jun 25, 2020 at 11:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.