the ^ operator

This is a discussion on the ^ operator within the C++ Programming forums, part of the General Programming Boards category; I saw in the q3 source code they use the ^ operator, so I tryed it on a console application ...

  1. #1
    Ben
    Guest

    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
    Location
    CA
    Posts
    2,459
    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
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ^ 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 -

    00001000

    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

    00001100

    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

    00000100

    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).
    zen

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

  5. #5
    Ben
    Guest

    Source code

    You can get it here:
    http://www.fileplanet.com/index.asp?file=53726

    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, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03: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-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21