CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: xor

  1. #1
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Question xor

    Hi folks. is there an XOR binary (bool,bool) operator in C++ please?

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Presumably you mean logical XOR as opposed to bitwise XOR (the ^ operator).

    No. Use:
    Code:
    (a || b) && !(a && b)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Jun 2002
    Location
    Dover, England
    Posts
    28
    A better way to do this is :
    ( a != b ).
    This will obviously only be true if one of a and b is TRUE and the other is FALSE.

    If you wanted to, you could even define XOR to be !=, as in the following:

    #define XOR !=

    int main(int argc, char* argv[])
    {
    bool a = ( false XOR false );
    bool b = ( false XOR true );
    bool c = ( true XOR false );
    bool d = ( true XOR true );

    return 0;
    }

    This approach makes the intention of the code just as clear as an inbuilt xor operator would.

  4. #4
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352
    ok thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured