Before jumping to the section you **must **have knowledge about **Bit** (**Bi**nary Dig**it** ) and also **two’s complement** in binary.`&`

Operator [AND]

It will return true if both operands have **1 **in its binary value.` 4 & 3`

will return true. Because decimal **4** is equivalent to **100** and decimal **3** is equivalent to **11** in binary. So, both the operands have **1**, therefore the condition will be true.`|`

Operator [OR]

It will return true if at least one operand has **1** in its binary value. `0 & 3`

will return true.`^`

Operator [XOR]

It will return true if operands are different.`5 ^ 4`

will return true.`<<`

and `>>`

Operator [Shift Bits]

It will move bits to LEFT or RIGHT. `echo 5 >> 1;`

will return **2. **Because **5**=**101** and shifting 1 character will result in **10** (binary) which decimal value is **2**. Similarly, `echo 4 >> 1;`

will result in **2.**`~`

Operator [Negate Bits]

It converts zeros into ones and vice versa.`echo ~2;`

will result in **-3**. But how? Let’s go to behind the scene.

decimal 2 = 10 binary

Let’s assume its 4 bits [Though PHP handles it as 32/62 bits].

So, `2`

= `0010`

Converting **0=>1** and **1=> 0**. `1101`

As you can see the value is not the real value, so we need to perform **two’s complement**.`1101`

=> `0010`

=> `0011`

so, real value is **0011** = **3** in decimal and the sign is **–**.

Therefore, the final output is `~2`

=`-3`

Still don’t get it? `~x = -x -1`

, remember this to assume the output for numerical values.

Categories: Uncategorized

## 0 Comments