Thread: Question about an operator

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Question about an operator

    Hello.
    In the following code, which can be found in this tutorial, what does the operator ^= mean?

    Code:
    #define SWAP(a, b)  a ^= b; b ^= a; a ^= b;
    Thanks in advance.

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    ^ is the symbol for the logical operator XOR (exclusive OR)

    a ^= b is the same as a = a ^ b

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Thanks. Appreciate it.
    So it means

    a equals exclusively a or exclusively b
    Interesting...
    So why then does it repeat the same thing two times, basically stating

    a equals exclusively a or exclusively b
    b equals exclusively a or exclusively b
    (and then it says it again)
    a equals exclusively a or exclusively b
    ?
    Last edited by Programmer_P; 05-10-2009 at 08:37 PM.

  4. #4
    Registered User Mediah-ikthya's Avatar
    Join Date
    May 2009
    Location
    Niceville
    Posts
    3
    stupid question, what is an exclusive or?

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Mediah-ikthya View Post
    stupid question, what is an exclusive or?
    I was going to say that there aren't any stupid questions, but to be honest, after spending years on a forum on computer security(Not this one), I have to say that there are.

    Yours, however, isn't.

    XOR is an operator(So to speak) that is part of the boolean logic system invented by George Boole.

    You should really look it up, as it's a much too complex subject for me to explain in a forum post, but let it suffice to say that the above code, due to the nature of the XOR operator, swaps the values. Also, boolean logic is pretty much the base of all circuitry ever made. Your computer is based on this stuff.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    Registered User Mediah-ikthya's Avatar
    Join Date
    May 2009
    Location
    Niceville
    Posts
    3
    Quote Originally Posted by IceDane View Post
    I was going to say that there aren't any stupid questions, but to be honest, after spending years on a forum on computer security(Not this one), I have to say that there are.
    Sadly there are stupid questions, I work in customer service and deal with them on a daily basis. I think that there people who simply should not be allowed to have computers. I had to try and explain to a woman that if her computer was displaying another language that had to do with the settings on her computer and it had nothing to do with her monitor.

    But all that aside

    So I did google it, which is what I probably should have done in the first place, and all I can say is what a neat function! I'm a complete noob slowly working my way through my first C++ class. I've just mastered the += function and the basic concept of switches.

    I thought I would start haunting programming boards to see what I can learn. I always seem to do better when I get in way over my head and am forced to sink or swim.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Mediah-ikthya View Post
    I always seem to do better when I get in way over my head and am forced to sink or swim.
    Same here. For some strange reason, I tend to learn more when asking a question on a forum, and get an answer, and go through each problem like that (if necessary; I do my best to learn the "normal" way), so its broken down, instead of learning by reading pages on a subject that is way over my head, though I always try to do that (and end up coming to the support forums, anyway, when I get stuck). I guess though if I didn't make an effort to read up first on the subject I'm trying to learn, I wouldn't even have any questions to ask, and so it at least serves as a motivator when I read up on the subject first. My brain seems to be geared to ask, and receive, type of learning. But, I eventually learn (by one method or another), and come back to the same board that helped me, to help other users that may be in the same boat I was.
    Last edited by Programmer_P; 05-10-2009 at 09:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new operator question
    By abachler in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2008, 02:06 PM
  2. Question about operator overloading
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:02 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 06:46 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM