简体   繁体   中英

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

I ran across a code challenge. After I finished it I looked at the other answers. I saw an answer that I am struggling to comprehend.

#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

bool willYou(bool young, bool beautiful, bool loved)
{
    return (young & beautiful) ^ loved;
}

TEST_CASE("willYou are computed", "[willYou]")
{
    REQUIRE(willYou(true, true, true) == false);
    REQUIRE(willYou(true, false, true) == true);
    REQUIRE(willYou(false, false, false) == false);
    REQUIRE(willYou(false, false, true) == true);
}

In case you want the information provided for the challenge (I don't think its necessary here) this is it.

Once Mary heard a famous song, and a line from it stuck in her head. That line was "Will you still love me when I'm no longer young and beautiful?". Mary believes that a person is loved if and only if he/she is both young and beautiful, but this is quite a depressing thought, so she wants to put her belief to the test.

Knowing whether a person is young, beautiful and loved, find out if they contradict Mary's belief.

A person contradicts Mary's belief if one of the following statements is true:

they are young and beautiful but not loved;
they are loved but not young or not beautiful.
Example

For young = true, beautiful = true, and loved = true, the output should be
willYou(young, beautiful, loved) = false.

Young and beautiful people are loved according to Mary's belief.

For young = true, beautiful = false, and loved = true, the output should be
willYou(young, beautiful, loved) = true.

Mary doesn't believe that not beautiful people can be loved.

Input/Output

[execution time limit] 0.5 seconds (cpp)

[input] boolean young

[input] boolean beautiful

[input] boolean loved

[output] boolean

true if the person contradicts Mary's belief, false otherwise.

What I am failing to grasp is how the ^ operator is used here to get the desired Boolean result? I created those test using Catch2 . From GeeksForGeeks it simply states...

The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

Well, pretty sure true and false here would be 1 and 0. So what I assumed was in the first test case would return 1 or true, but it is false or 0? If all are true, why is it false? I get that the story is trying to explain this here. But in the story it is saying if young and beautiful, then you are loved. I'm not sure if I am misunderstanding that statement, or if it's just bad explanation (I'm starting to think it's just bad). Anyhow, I need help trying to understand this operator more and basically grasp what all this means. I am just all but lost here.

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

Use [in]equality operators ('==' as xnor, and '!=' as xor);

bool a,b;

...

bool c=a==b;//xnor

bool d=a!=b;//xor

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