Thread: the ^ operator

  1. #1

    Question the ^ operator

    I saw in the q3 source code they use the ^ operator, so I tryed it on a console application and found out that it does the same as addition. Doing "x ^= 8" appears to do the same as "x += 8". I was wondering why there are two operators for the same operation. Thanks.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    the '^' operator is actually the exclusive-or [XOR] bitwise operation... coincidentally... if x is zero... x ^= 8 yeilds the same result as x += 8...
    hasafraggin shizigishin oppashigger...

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    ^ is the exclusive OR operator. You're partially correct in that it will add. But only if the bit you're adding is not set in the value your are adding to. 8 has a binary representaion of -


    if you exclusive OR it with any value that doesn't have the fourth bit set then you will add it ( same as normal OR) but if you XOR it with


    then if one bit or the other are set then it will be set in the result, but if both bits are set then they will be cleared. So if you XOR'ed(^) the above two values, you'd get


    I'm not sure how Quake 3 uses XOR, but quite a few games XOR some of the variables they use from primitive encriptition to try and prevent trainers and cheats to be made for them (by those who want to dissassemble them). If you XOR a value with a key you decode it and if you XOR this decoded valued with the original key then you get the original value back again.

    My signature below can be decoded in a similar way (the text is the key to the ASCII values).

  4. #4
    just curious, where did you see the q3 source?

  5. #5

    Source code

    You can get it here:

    Have fun with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM