N
N
Novice232018-03-12 15:06:52
C++ / C#
Novice23, 2018-03-12 15:06:52

How do Boolean expressions work?

int x = 1984;
int y = 2001;
x ^= y ^= x = y;
Console.WriteLine("x = " + x + "; y = " + y);

How does this boolean expression work?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Eremin, 2018-03-12
@EreminD

That's it, that's
it

int x = 1984;
int y = 2001;
string binaryX = Convert.ToString(x, 2);
string binaryY = Convert.ToString(y, 2);
Console.WriteLine("binaryX = " + binaryX + "; binaryY = " + binaryY);
x ^= y ^= x  = y;
Console.WriteLine("x = " + x + "; y = " + y);

binaryX and binaryY are binary representations of numbers
The ^ operator takes two numbers and does a bitwise exclusive, or
The ^= operator performs an exclusive or on the left and right arguments and writes the result immediately to the left (like the *=, +=, etc. operators)
As for the x = y tail, remember that the = operator also returns a value. For check:
int a;
Console.WriteLine(a = 3);

Well, do not forget about the priority of the execution of operations

N
Novice23, 2018-03-12
@Novice23

Not quite clear.
x ^= y ^= x = y; how is it calculated??

C
cicatrix, 2018-03-12
@cicatrix

Do you have a typo there?
It looks a lot like exchanging values ​​via XOR, which, according to the insidious idea of ​​the author of the question (from the textbook?), should have been confusingly the following:
x ^= y
y ^= x
x ^= y
After these three operations, x and y will exchange values

F
freeExec, 2018-03-12
@freeExec

A puzzle with a trick. If we expand it, we get:

int x1 = y;
y = y ^ y;
x = x ^ y;

As a result, Y is always 0, and X remains the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question