[英]What does the ^ operator do to a BOOL?
What does this statement mean? 这句话是什么意思?
isChecked = isChecked ^ 1;
isChecked
is a BOOL
. isChecked
是一个BOOL
。
The "^" is an exclusive OR operation, so 0 flips to 1, and 1 flips to zero. “^”是异或运算,因此0翻转为1,1翻转为零。 The result should be the same as isChecked = !isChecked
. 结果应与isChecked = !isChecked
相同。
它将XOR与1检查,所以我认为是真的^ 1 = 0(假)和假^ 1 = 1(真)
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
. 它只翻转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
. 如果某人足够疯狂将BOOL
变量设置为某个数字,例如5.然后执行^ 1
只会将值的最后一位翻转为4,仍然会将其评估为YES
。
If you want to logically negate, use !
如果你想在逻辑上否定,请使用!
operator instead. 而不是运营商
^
is the exclusive or operator. ^
是独家或运营商。
In your example it is used to create a toggle - isChecked
will be set only if isChecked
was previously unset. 在您的例子是用于创建切换- isChecked
将设置只有isChecked
以前未设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.