Thread: I don't get how the following expression gets simplified

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    I don't get how the following expression gets simplified

    I was told the following
    Code:
    if ( (a && b) || (!a && !b) )
    was equivalent to

    Code:
    if(!!a == !!b)
    Can someone explain to me how they arrive at this answer? I really don't see the steps involved.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Overworked_PhD View Post
    I was told the following
    Code:
    if ( (a && b) || (!a && !b) )
    was equivalent to

    Code:
    if(!!a == !!b)
    Can someone explain to me how they arrive at this answer? I really don't see the steps involved.
    The first expression evaluates to true if both a and b are true, or if neither a nor b are true. Since any non-zero value is true, then you cannot just condense this to "a == b". But if you double-negate, then a true value negates to zero, then negates to one, and then the comparison will work.

    If only we had a logical XOR (as opposed to bitwise), we could say:

    Code:
    if( ! ( a ^^ b ) )
    But alas we don't.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Okay. I get it.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It could be further simplified to either:

    !a == !b

    - or -

    !( !a ^ !b )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sebastiani View Post
    It could be further simplified to either:

    !a == !b

    - or -

    !( !a ^ !b )
    The first one is going to be the faster of those two, I'm pretty sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM