I ran into this bit of code today:
indexValid &= x >= 0;
What does the &= mean? Could someone explain what is occurring in this statement?
This is not about Objective-C, but regular C.
Here the statement with the &=
operator is equivalent to indexValid = indexValid & (x >= 0)
.
The &
operator itself is called the bitwise and operator, and AND
s the operands. Which means, returns 1
only if both operands are 1
, else returns 0
if any of the operands is not 1
. AND
ing and OR
ing is commonly used in setting flags in software.
For example, if indexValid
is 0011010
in binary and you AND
it with (x >= 0)
(which is a boolean expression result, either 1 or 0), the result is 0000000
and (let's say x >= 0 evaluates to 1) as 0011010 & 0000001
evaluates to 0000000
.
If you don't know about binary logic, http://en.wikipedia.org/wiki/Boolean_logic is a good source to start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With