Why does "x ^ = true" produce false in this example?

Why does the operator z ^ = true create false when the previous creates true?

bool v = true; bool z = false; z ^= v; Console.WriteLine(z); z ^= true; Console.WriteLine(z); OUTPUT ====== True False 
+4
source share
6 answers

Because:

 false ^ true == true true ^ true == false 

See http://en.wikipedia.org/wiki/Xor

+15
source

Because it changes the value of z in the first statement.

+20
source

^ The values โ€‹โ€‹of XOR, XOR are defined as true if one, but not both sides, are true and in each other case are defined as false.

So,

z ^ = v means z = false ^ true which means true

z ^ = true means z = true ^ true, which is not true

Note that ^ = changes the value of the variable in the first and second statements

+7
source

The truth table for XOR ( ^ ) -

 aba^b 0 0 0 0 1 1 1 0 1 1 1 0 

The operation lhs ^= rhs is basically just short for lhs = lhs ^ rhs . So, when you first use ^= you change the value of z , which (in accordance with the definition of ^ ) changes the result of the second application.

+2
source

false XOR true = true, then you set z to true; true XOR true = false, then you set z to false.

+1
source

An expression of the form x ^= y is evaluated as x = x ^ y

The result x ^ y (XOR) is true if and only if exactly one of its operands is true .

conclusion: x ^ = true will return true when x == true.

+1
source

All Articles