C Programming - XOR Bitwise Operation

ぐ巨炮叔叔 提交于 2019-12-28 07:17:32

问题


What operation does the following ‘C’ statement perform?

star = star ^ 0b00100100;

(A) Toggles bits 2 and 5 of the variable star.

(B) Clears all bits except bits 2 and 5 of the variable star.

(C) Sets all bits except bits 2 and 5 of the variable star.

(D) Multiplies value in the variable star with 0b00100100.

I'm still clueless about this. Can someone help me out?


回答1:


XOR operator (also called "logical addition") is defined like this:

a   b   a^b
-----------
0   0    0
0   1    1
1   0    1
1   1    0

So a^0 leaves a intact while a^1 toggles it.

For multiple-bit values, the operation is performed bitwise, i.e. between corresponding bits of the operands.




回答2:


If you know how XOR works, and you know that ^ is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.

From an "during the test" standpoint, let's say you need to prove this to yourself, you really don't need to know the initial value of star to answer the question, If you know how ^ works then just throw anything in there:

 00100100
^10101010  (star's made up value)
---------
 10001110  (star's new value)

 bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  
               |---|---|---|---|---|---|---|---
 star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
               |---|---|---|---|---|---|---|---
 star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0

Then check your answers again, did it:

(A) Toggles bits 2 and 5 of the variable star. (Yes)

(B) Clears all bits except bits 2 and 5 of the variable star. (Nope)

(C) Sets all bits except bits 2 and 5 of the variable star. (Nope)

(D) Multiplies value in the variable star with 0b00100100. (36x170 = 142? Nope)




回答3:


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.




回答4:


The exclusive OR has this truth table:

A   B   A^B
-----------
1   1   0
1   0   1
0   1   1
0   0   0

We can see that if B is true (1) then A is flipped (toggled), and if it's false (0) A is left alone. So the answer is (A).




回答5:


XOR operator returns 0 if both inputs are same otherwise returns 1 if both inputs are different.For Example the Given Truth Table :-

  • a=1 b=1 => a^b=0,
  • a=0 b=0 => a^b=0,
  • a=0 b=1 => a^b=1,
  • a=1 b=0 => a^b=1.



回答6:


well xor is binary operator that work on bits of 2 nos.

rule of xoring:for same bit ans is 0 and for different bit ans is 1 let

a= 1 0 1 0 1 1
b= 0 1 1 0 1 0
--------------
c= 1 1 0 0 0 1
--------------

compare bit of a and b bit by bit if same put 0 else put 1 xor is basically used to find the unique in given set of duplicate no. just xor all nos. and u will get the unique one(if only single unique is present)



来源:https://stackoverflow.com/questions/16941330/c-programming-xor-bitwise-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!