Logical Xor Operator in C++

Logical XOR operator in C++?

The != operator serves this purpose for bool values.

Is there a logical (boolean) XOR function in C or C++ standard library?

Boolean XOR is the same thing as !=, "not equal."

p | q | p != q
--+---+-------
F | F | F
T | F | T
F | T | T
T | T | F

http://en.wikipedia.org/wiki/Truth_table#Logical_conjunction

What is the point of the logical operators in C?

You don't need logical XOR, I have forgotten the SO question, but it's similar to what you're thinking, basically we don't need XOR, it's equivalent to != anyway

FALSE XOR FALSE == FALSE
FALSE XOR TRUE == TRUE
TRUE XOR FALSE == TRUE
TRUE XOR TRUE == FALSE

FALSE != FALSE == FALSE
FALSE != TRUE == TRUE
TRUE != FALSE == TRUE
TRUE != TRUE == FALSE

I'll search my favorites, and paste here the link later...

The ^ (bitwise XOR) in C++ with Boolean

I think I get what you are trying to ask. I saw the comments section trying to explain you what a XOR does and it was kind of funny. Anyway,

The problem states that, Mary's belief was: "if you are young and beautiful, you are loved". Our goal is to contradict Mary and show her that there were times when someone was loved but did not have one of the two or both the qualities, OR if someone did have those qualities but was not loved.

In a nutshell, our aim is to contradict her beliefs.

The output of the willYou() function is to check if we can contradict Mary or not. Maybe you might have solved this question by taking in various cases and having an output for each. The solution mentioned in your question is a generic one.

The XOR operator returns 1 if there are an odd number of 1's in the expression.
For instance:

  1. if young = 1 and beautiful = 1, their AND is 1 and their XOR with loved = 1 is 0(i.e. false) and hence it does not contradict Mary.
  2. if young = 0 and beautiful = 1, their AND is 0 and their XOR with loved = 1 is 1(i.e. true) and hence it does contradict Mary.

Similarly, one can form this for other cases as well.

The solver must've used the "passer/inverter" property of XOR. Whenever the outputs were in Mary's favour, it inverted the answer to yield us false meaning we cannot contradict her. I think it would make our lives easier to say that it is just a creative solution made by someone and maybe not a mind-boggling problem(not trying to be condescending at all here).

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to booleans, then != is xor.

bool(a) != bool(b)

Is the ^ operator really the XOR operator in C#?

It is not the power of operator of C# since there is no such operator in C#. It is just the XOR operator.

For "power of", use Math.Pow.

As you can see from this page on the C# Operators, ^ is listed under the "Logical (boolean and bitwise)" category, which means it can both handle boolean values, and binary values (for bitwise XOR).

How do you do logical exclusive XOR in the C preprocessor with defines

you can solve this by checking if both are defined equally. so 0 0 and 1 1 will throw the error, while 0 1 and 1 0 are allowed.

#if defined(USE_OPTION1) == defined(USE_OPTION2)
#error "You must use at least one option, but not both"
#endif

XOR operation in C++

Because of the operator precedence and because the xor is a binary operator, which in this case is left-to-right.

First 1 ^ 3 is evaluated

0 0 0 1 // 1

0 0 1 1 // 3
-------
0 0 1 0 // 2

The result is 2, then this number is the first operand of the last xor operation (2 ^ 7)

0 0 1 0 // 2  

0 1 1 1 // 7
-------
0 1 0 1 // 5

The result is 5.



Related Topics



Leave a reply



Submit