What does &= mean in objective c?
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.
It is the bitwise AND
plus assignment operator
(or 'and accumulate').
It combines a bitwise AND
against the left-hand operand with assignment to said operand.
x&= y;
is
x= x & y;
链接地址: http://www.djcxy.com/p/72652.html
上一篇: 这句话中&符号(&)的含义是什么?
下一篇: 目标c中的&意味着什么?