# explain this line of code?

## Recommended Posts

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

##### Share on other sites
It reverses the least significant bit through the use of a bitwise XOR.

##### Share on other sites
ah ok, so it reverses a boolean? that makes sense. i was reading it as "key = key to the power of one"

##### Share on other sites
toggle[key] = 0011011           = 000001              ^ (xor)---------------------            = 001100

turn off..... bits

##### Share on other sites
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

##### Share on other sites
That's not the same though. ^ is a bitwise operator. It only flips individual bits, not the entire boolean expression.

##### Share on other sites
Quote:
 Original post by SpoonbenderThat'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 '!'...

##### Share on other sites
I would use razors one, its clear what is intended.

##### Share on other sites
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 !.

##### Share on other sites
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 '^'...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628318
• Total Posts
2982044

• 9
• 9
• 13
• 11
• 15