What does the ^ operator do to a BOOL?

后端 未结 7 1703
情深已故
情深已故 2021-01-23 04:10

What does this statement mean?

isChecked = isChecked ^ 1;

isChecked is a BOOL.

相关标签:
7条回答
  • 2021-01-23 04:27

    "^" 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

    0 讨论(0)
  • 2021-01-23 04:29

    it will XOR isChecked with 1 so I suppose true ^ 1 = 0(false) and false ^1 = 1(true)

    0 讨论(0)
  • 2021-01-23 04:31

    this is bitwise XOR operator and changes 0 to 1, and 1 to zero. see all opertors here.

    0 讨论(0)
  • 2021-01-23 04:33

    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.

    0 讨论(0)
  • 2021-01-23 04:41

    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

    0 讨论(0)
  • 2021-01-23 04:49

    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.

    0 讨论(0)
提交回复
热议问题