C Programming - XOR Bitwise Operation

后端 未结 6 1160
傲寒
傲寒 2020-12-07 06:01

What operation does the following ‘C’ statement perform?

star = star ^ 0b00100100;

(A) Toggles bits 2 and 5 of

6条回答
  •  我在风中等你
    2020-12-07 06:20

    It is (A) toggles bits 2 and 5.

    The following is the truth table for the XOR operation:

    x  y  x^y
    0  0   0
    1  0   1
    0  1   1
    1  1   0
    

    You can see from the table that x XOR 0 = x and x XOR 1 = !x.

    XOR is a bitwise operation, so it operates on individual bits. Therefore if you XOR star with some constant, it will toggle the 1 bits in the constant.

    You can find some explanation e.g. here.

提交回复
热议问题