CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    May 2013
    Posts
    2

    Re: Logical XOR in C/C++?

    Quote Originally Posted by NMTop40 View Post
    your best way to do it would be

    (!b1 ^ !b2)


    where b1 and b2 are expressions.
    This does not implement XOR. Try substituting b1<-0, b2<-0
    You can also see it in the xor karnaugh map which cannot be simplified

    simplest way is to use bitwise ^ or (!b1&&b2)||(b1&&!b2)

    cheers!

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Logical XOR in C/C++?

    Quote Originally Posted by juncode
    This does not implement XOR. Try substituting b1<-0, b2<-0
    If we use the rules from C: (!(-1) ^ !(-2)) = (0 ^ 0) = 0. This is correct.

    Quote Originally Posted by juncode
    You can also see it in the xor karnaugh map which cannot be simplified
    I do not see how that matters here since the proposed alternative expression is not a simplification.

    Quote Originally Posted by juncode
    simplest way is to use bitwise ^
    It was already pointed out that this would not work in general, back in post #4, some 12 years ago.
    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

  3. #18
    Join Date
    May 2013
    Posts
    2

    Re: Logical XOR in C/C++?

    Of course. I confused ^ with &&. Clearly !bn gives a result of type bool, which can be then correctly used with bitwise xor ^.

    Sorry and thanks for pointing that out!

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Logical XOR in C/C++?

    It depends how you define 'correctly'
    the result of 2 bools being bitwise xor'ed is an int. if you expect the result being of type bool, then you may not be getting the result you want. the "correctest" approach for logical xor behaviour is really using a != operator.
    with != either of the 2 operators is evaluated exactly once, and the result is a bool.


    Using a != will probably also better convey what the code is supposed to do, making it more readable. The only exception I can think of to this is probably trying to actually model logic gates in a circuit.

  5. #20
    Join Date
    Oct 2018
    Posts
    2

    Re: Logical XOR in C/C++?

    I'm curious as to whether ...

    if ((a == b) != (c == d))

    is equivalent to:

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

    were the '^^' operator to exist (i.e. the elusive exclusive or operator is just '!=')?

  6. #21
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Logical XOR in C/C++?

    For logical bool != we have the truth table

    0 0 -> 0
    1 0 -> 1
    0 1 -> 1
    1 1 -> 0

    This is indeed the logical exclusive or (^^ as defined above in post #20)

    So in this case, (a == b) != (c ==d)
    would give the same logical bool result as (a == b) ^^ (c == d) if ^^ was defined as the logical bool exclusive or.

    Note that in this case, the operator ^^ is operating on 2 bools so in this case the bitwise exclusive or (^) would produce the same result

    so (a== b) != (c == d)
    gives the same result as
    (a==b) ^ (c==d)

    Note this is only true when the 2 values being compared with != are bool. For any other type ^ is not equal to !=.
    Last edited by 2kaud; October 3rd, 2018 at 12:33 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    Join Date
    Oct 2018
    Posts
    2

    Smile Re: Logical XOR in C/C++?

    Thanks for your reply 2kaud ...

    You are correct and also pointing out this was implied in post 20 - I did not intend to plagiarise - I just stumbled on this thread after working it out (and trying it out) for myself.

    I guess the key point (answering your last) is that &&, ||, etc. are logical operators intended for use with Boolean variables anyway ...

    So the answer to the post in it's simplest form:

    "Logical XOR in C/C++?" is...

    Yes it's '!='.

    ... furthermore '==' is a Logical XNOR.



Page 2 of 2 FirstFirst 12

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