简体   繁体   中英

C Programming - XOR Bitwise Operation

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?

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, ie between corresponding bits of the operands.

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)

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 eg here .

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).

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM