What does ^= mean?

Started by
8 comments, last by Quiggy 21 years, 7 months ago
Ok, I know most of the C++ equal sign meanings, but this one stumps me. I saw it in a tutorial that I got from the net, but it dosn''t say what it does and the book I''m reading dosn''t have it in there either. Can anybody enlighten me?
Advertisement
"^" is the exclusive OR operator (aka XOR), so "^=" is "exclusive OR equals" operator.

A = A ^ B; // is the same as

A ^= B;

Enjoy.
This is an operator like +=, -= and *=
a^=b;
is the same as
a=a^b;

The ^ operator is a bitwise XOR:
010010101
101011011
--------- ^
111001110
Bitwise-exclusive-OR assignment.
See MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsoprxorequ.asp
Ahh, ok. Thanks guys. Thought it would be something like that. That bit of code actually makes sense now.

Thanks again for the snappy response. I love this board!

well the " ^ " is the XOR symbol also called exclusive OR.

example: 1001 ^ 1001 = 0000
1011 ^ 0111 = 1100
why? you may ask.

in XOR if the two bits being XORed are both true (1) or both false (0) then the result is false (0), otherwise it is true (1).

so if number has the value 1011. then...

number = 1011;

number = number ^ 0111; // number now equals 1100
number ^= 0111; // this is the equivalent to the above and it still
// equals 1100

other similar examples:

add = add + 1; is the same as add += 1;
integer = integer / 5; is the same as integer /= 1;

see?

hope this helped?

if i''m wrong please don''t chastise me

Beginner in Game Development?  Read here. And read here.

 

at least three people said the same thing (and added something unique too), one of them after the original poster said "thanks, i got it" (probably took him long to type that up). not very efficient, i have to admit. =^

---
shurcool
wwdev
Hey, I''d rather have an over abundance of messages then none. I thought it was great. I find the people on this board are very helpfull and I''m sure going to take advantage of it.

Although to be perfectly honest I don''t really understand why you would want to compare 100101 to 001011 for example. I''m sure once I''ve coded a bit more and understand more it''ll make more sense but right now I don''t really see a use for it.

The example I was originally puzzled about was an instance in the tutorial where the writer was using

turn ^= 1; / 1 = human 0 = computer

to assign the next players turn. It replaced an if statment. This I can understand now thanks to you guys, but why would you want to compare a bunch of zero''s and ones like you guys were showing me in your replies? Like I said earlier it''ll probably make sense one day, but just not right now.
Cryptography and Encryption, this makes it very hard to figure out.
look here > http://gamedev.net/reference/articles/article1630.asp

[edited by - monkey_32606 on September 21, 2002 5:03:32 PM]
Quote:Michael TanczosCut that shit out. You shouldn't be spying on other people.. especially your parents. If your dad wanted to look at horses having sex with transexual eskimo midgets, that's his business and not yours.
in your game programming adventures you will discover that you will have to manipulate numbers and file by their individual bits.

this is why there were all those 0s and 1s in the post. like in the example.

turn ^= 1; // turn = turn ^ 1;

you are manipulating that number on the bit level. which in this case the bit is exactly 1 or 0.

anyway see you said you understand, i''ll leave it at that.

have fun.

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement