Practice mode C Language

Which statement sets bit n of an unsigned integer x to 1 and leaves the other bits unchanged?

Correct answer D. x |= 1u << n;

Explanation

OR with a mask that has only bit n set forces that bit to 1 and leaves every other bit as it was, because OR with 0 changes nothing. x &= ~(1u << n) is the matching idiom for clearing the bit, and ^= toggles it, which clears it if it was already set. x &= 1u << n would clear all the other bits.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.