Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. No conditional XOR in C++/C#?

No conditional XOR in C++/C#?

Scheduled Pinned Locked Moved The Lounge
5 Posts 3 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I've always wondered why there is no XOR in C++, and again in C#? There is the Logical XOR (^), but no "^^"

    E J 2 Replies Last reply
    0
    • L Lost User

      I've always wondered why there is no XOR in C++, and again in C#? There is the Logical XOR (^), but no "^^"

      E Offline
      E Offline
      Erik Funkenbusch
      wrote on last edited by
      #2

      XOR doesn't makes sense in any useage other than a bitwise XOR. How would this work? If (true ^^ false) DoSomething(); what would be the result? And how would it differ from: if (true ^ false) ?

      L 1 Reply Last reply
      0
      • E Erik Funkenbusch

        XOR doesn't makes sense in any useage other than a bitwise XOR. How would this work? If (true ^^ false) DoSomething(); what would be the result? And how would it differ from: if (true ^ false) ?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What do u mean? I want to check if something changed for example.. I have a bool value, and if that value changes, I want to take action. What u can do is: bool b=m_mybool; m_mybool=GetBoolNewValue(); [...] if(b ^^ m_mybool) { Do something or another. } To answer your question: if(true ^ false) is exactly the same as if(true ^^ false). However, if(a ^ b) is not the same as if(a ^^ b) - remember - any non zero value is true - a ^ b is the same as a ^^ b only when both a and b have only 1 and 0 as values. The previous example I showed would have worked just as well if I had written if(b != m_mybool). HOWEVER, this is true ONLY when the types are of type bool - had the types been of type int or char* or some such, I would not have been able to do that. If, for example, the check I wanted to do is : "if only one of the strings is a valid string", I have to resort to if(((st1==NULL) && (st2!=NULL)) || ((st1!=NULL) && (st2==NULL))). Had I had the operator ^^, I could have simply written: if(st1 ^^ st2). Get it? Shahar

        L 1 Reply Last reply
        0
        • L Lost User

          What do u mean? I want to check if something changed for example.. I have a bool value, and if that value changes, I want to take action. What u can do is: bool b=m_mybool; m_mybool=GetBoolNewValue(); [...] if(b ^^ m_mybool) { Do something or another. } To answer your question: if(true ^ false) is exactly the same as if(true ^^ false). However, if(a ^ b) is not the same as if(a ^^ b) - remember - any non zero value is true - a ^ b is the same as a ^^ b only when both a and b have only 1 and 0 as values. The previous example I showed would have worked just as well if I had written if(b != m_mybool). HOWEVER, this is true ONLY when the types are of type bool - had the types been of type int or char* or some such, I would not have been able to do that. If, for example, the check I wanted to do is : "if only one of the strings is a valid string", I have to resort to if(((st1==NULL) && (st2!=NULL)) || ((st1!=NULL) && (st2==NULL))). Had I had the operator ^^, I could have simply written: if(st1 ^^ st2). Get it? Shahar

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I agree that it seems a little strange to not throw in a ^^ operator, but I don't think I would use it very often if it did exist. Maybe its just not a very common logic operation for the common programmer. If you find yourself needing one a lot, its a good thing c++ is so extensible: #define XOR(a,b) (!(((a) && (b)) || (!(a) && !(b)))) thats not as pretty as ^^ but i think it works.

          1 Reply Last reply
          0
          • L Lost User

            I've always wondered why there is no XOR in C++, and again in C#? There is the Logical XOR (^), but no "^^"

            J Offline
            J Offline
            Jonathan Gilligan
            wrote on last edited by
            #5

            Because != is the same as XOR for logical types: if ((a < 1) != (b > 2)) returns true if one and only one of the two statements is true.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups