简体   繁体   English

^运算符对BOOL做了什么?

[英]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以前未设置。

this is bitwise XOR operator and changes 0 to 1, and 1 to zero. 这是按位XOR运算符,将0更改为1,将1更改为0。 see all opertors here . 看到所有的opertors 在这里

"^" is called an exclusive OR or XOR operation. “^”称为异或或异或运算。 In this case, it will change boolean from true to false and vice-versa. 在这种情况下,它会将布尔值从true更改为false,反之亦然。

To learn more on this, check this link 要了解更多信息,请查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM