Logical XOR in C/C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Logical XOR in C/C++?

  1. #1
    Join Date
    Nov 2001
    Posts
    1

    Logical XOR in C/C++?

    What is the LOGICAL (not binary) XOR operator in C++? You know, like (a && b) or (x || y) ... ?

    I know I can use ((a || b) && !(a && b)) but as I need to call a fxn and burn cycles to evaluate one of the operands that is a kludge ...



  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Logical XOR in C/C++?

    There is no logical XOR in C++.

    Regards,

    Paul McKenzie


  3. #3
    Join Date
    Jul 2001
    Location
    Mumbai,India
    Posts
    382

    Re: Logical XOR in C/C++?

    The EXOR operator is ^(caret).

    Regards,
    The Beret.


  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Logical XOR in C/C++?

    The ^ XOR symbol performs a numeric XOR of two numbers (but not a logical one).

    Thus 1 ^ 2 == 3

    Whereas a logical XOR(a,b) would return zero if either both or neither of a and b are zero.

    For this there is no operator in C++.





  5. #5
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Logical XOR in C/C++?

    There is no logical XOR in C++, mainly because, unlike AND and OR, XOR cannot be "short-curcuit" evaluated: With AND and OR, once you've evaluated the first operand, half the time you'll know if there's any need to evaluate the second operand. With XOR, this cannot be done. Both operand must always be evaluated.

    In other words:
    if ( (a==b) & (c==d) )


    generates different code from
    if ( (a==b) && (c==d) )


    while
    if ( (a==b) ^ (c==d) )


    would generate exact the same code as
    if ( (a==b) ^^ (c==d) )


    if the latter were a valid syntax.

    The solution: Use a bitwise XOR on the boolean operations, as done in the third example above.


    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Logical XOR in C/C++?

    your best way to do it would be

    (!b1 ^ !b2)


    where b1 and b2 are expressions.

    That is, assuming that ! will return the same boolean type for both.

    int a=1;
    int b=2;

    assert (! ( !a ^ !b ));






  7. #7

    Re: Logical XOR in C/C++?

    bitwise or " | "

    bitwise exlusive or " ^ "

    logical or " || "

    "To have a lack of imagination, not only shows your lack of intelligence, but also keeps the human race in the shadow of weakness"


    -Ulberon

  8. #8
    Join Date
    May 2000
    Location
    Birmingham, England
    Posts
    213

    Re: Logical XOR in C/C++?

    Using boolean algebra heres an XOR, bit messy but, it will do#define XOR(exp1,exp2) (!((exp1)||(exp2)) || (exp1) && (exp2))

    where exp1 is expression one, and exp2..well you get the idea.
    Sorry about all the brackets but macro functions need them.

    The theory if you need it
    XOR = ^a.^b + a.b
    XOR = ^(a+b) + a.b (deMoivre)
    Just a touch more efficient than the first,right now I'm going home to sleep

    Stitch
    ====================

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Logical XOR in C/C++?

    easier is (!!(a) ^ !!(b))

    The first ! will always produce a numeric type, but you can't guarantee the value of "true".

    However, the compiler should give the same "true" value for ! on two numeric types that are both 0.
    At least I would assume it does. Thus the second one.

    It's a shame you cannot define your own binary operators, thus you cannot create one called ^^



  10. #10
    Join Date
    Jan 2000
    Location
    Las Vegas, NV, USA
    Posts
    470

    Re: Logical XOR in C/C++?

    What does short-circuit evaluation of logical expressions have to do with the non-implementation of a logical XOR in C++ or C?

    -n


  11. #11
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Logical XOR in C/C++?

    Basically, the purpose of having both logical and bitwise AND and OR is to allow short-circuit evaluation on the logical expressions. Other wise, you could get by with just the bitwise operators.

    You'll note that (as far as I know), no other computer language offers both a bitwise and logical XOR (most just offer bitwise AND and OR and use that for logical expressions)

    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

  12. #12
    Join Date
    Jan 2000
    Location
    Las Vegas, NV, USA
    Posts
    470

    Re: Logical XOR in C/C++?

    I stand corrected. Of course XOR cannot be short-circuited, and to have it as a logical operator would require preventing AND and OR from being short-circuited as well, to maintain language semantics, and a consistent behavior across all logical operators.

    -n


  13. #13
    Join Date
    Oct 2009
    Posts
    1

    Re: Logical XOR in C/C++?

    You can use the != operator instead of the XOR in C++,
    i.e. (a XOR b) is equivalent to (a != b) assuming a and b are boolean operands

  14. #14
    Join Date
    May 2009
    Posts
    2,413

    Re: Logical XOR in C/C++?

    Quote Originally Posted by KotBegemot View Post
    You can use the != operator instead of the XOR in C++,
    i.e. (a XOR b) is equivalent to (a != b) assuming a and b are boolean operands
    That's right.

    Anyway you must have broken some unofficial record here. This thread is over 8 years old and that's a long time even for a zombie thread.

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,289

    Re: Logical XOR in C/C++?

    Quote Originally Posted by nuzzle
    Anyway you must have broken some unofficial record here. This thread is over 8 years old and that's a long time even for a zombie thread.
    Heheh, but I guess that one thing might have changed after 8 years: the compilers more commonly in use these days are more standard compliant, so NMTop40's point about not being able to guarantee the value of !x where x is of a built-in or pointer type will no longer hold since C++ guarantees that its result will either be true or false, and true is implicitly convertible to one while false is implicitly convertible to zero, while C guarantees that it will either be 1 or 0.

    Oh, and we can more confidently replace Stitch's suggestion of a function-style macro with an inline function template.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center