What does this statement mean?
isChecked = isChecked ^ 1;
isChecked
is a BOOL
.
"^" is called an exclusive OR or XOR operation. In this case, it will change boolean from true to false and vice-versa.
To learn more on this, check this link
it will XOR isChecked with 1 so I suppose true ^ 1 = 0(false) and false ^1 = 1(true)
this is bitwise XOR operator and changes 0 to 1, and 1 to zero. see all opertors here.
The "^" is an exclusive OR operation, so 0 flips to 1, and 1 flips to zero. The result should be the same as isChecked = !isChecked
.
Everyone is saying it XORs the bool-- that's true-- but the purpose here is that it's toggling the bool.
The advantage of doing a bitwise toggle like this is speed and the ability to fiddle bits in extreme detail.
for more Bitwise Operators
It only flips the last bit of BOOL
. Not a reliable way to logically negate.
If someone is crazy enough to set the a BOOL
variable to some number, for example 5. Then doing ^ 1
will only flip the last bit of the value to 4, which is still evaluated to YES
.
If you want to logically negate, use !
operator instead.