explain this line of code?

Started by
8 comments, last by raz0r 18 years, 1 month ago
a nice easy one for you all today! i dont see what purpose the following line of code has, if i'm reading it right it doesnt actually do anything? toggle[key] ^= 1; thanks
Advertisement
It reverses the least significant bit through the use of a bitwise XOR.
ah ok, so it reverses a boolean? that makes sense. i was reading it as "key = key to the power of one"
toggle[key] = 0011011           = 000001              ^ (xor)---------------------            = 001100



turn off..... bits

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

 

You can also use '!' if you want to reverse a boolean's value.

For example,

bool bDummy = bool();bDummy = !bDummy; //bDummy -> truebDummy = !bDummy; //bDummy -> false
That's not the same though. ^ is a bitwise operator. It only flips individual bits, not the entire boolean expression.
Quote:Original post by Spoonbender
That's not the same though. ^ is a bitwise operator. It only flips individual bits, not the entire boolean expression.


Yes, but I was just pointing out that if all he wanted to do was change a boolean's value, he could use '!'...
I would use razors one, its clear what is intended.
A     B      A xor B0     0         00     1         11     0         11     1         0

Being bitwise, it goes bit by bit, so you shouldn't try to compare it with a logical operator, such as !.
As I've stated above, I was just POINTING OUT that if he wanted to change a boolean's value, he could use '!', that's all... I wasn't comparing '!' with '^'...

This topic is closed to new replies.

Advertisement