Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does &= mean in objective c?

Tags:

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?

like image 478
guy8214 Avatar asked Jul 20 '14 20:07

guy8214


1 Answers

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 ANDs the operands. Which means, returns 1 only if both operands are 1, else returns 0 if any of the operands is not 1. ANDing and ORing 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.

like image 96
Can Poyrazoğlu Avatar answered Sep 23 '22 22:09

Can Poyrazoğlu