What does ^= mean and how do I use it?

I've seen this operator in c++ codes and I don't know
what it means.




Thanks for answers in advance.
Not sure what XOR means, but, thanks.
it is exclusive or.
'regular' logical 'or' of 2 bits means that so long as both bits are not false, the answer will be true.
'exclusive or' means that if both are false it is false, and also if both are true, it is false.
xor reverses itself. that is, (x ^ y) ^y = x; and this property is very important.

you should be able to find out a lot more online, including looking at a truth table for OR and XOR would be useful for you.

In the example code you are looking at, the reversible property is used for encryption.
y is a 'secret' and x is the input data. if you xor it once, you get changed data, seemingly random, and if you xor it again, you get the original back, this is why encrypt and decrypt are the same.
I usually code that up as
seed random with numeric hash of the 'password' from user
encrypt each data byte with next random byte from random generator
that same logic decrypts the data, if password matched, because of the reversible property.
This type of encryption can be broken by reverse engineering the program, so its only moderately secure, good enough to stop someone who is not determined, and no good at all against a serious attack.
Last edited on
Ah, thanks for explaining this.

I'll try using this in my code.
Topic archived. No new replies allowed.