PHP "&" operator

It is the bitwise shift operator. Specifically, the left-shift operator. It takes the left-hand argument and shifts the binary representation to the left by the number of bits specified by the right-hand argument, for example:

1 << 2 = 4

because 1 (decimal) is 1 (binary); left-shift twice makes it 100 which is 4 in decimal.

1 << 5 = 32

because 100000 in binary is 32 in decimal.

Right shift (>>) does the same thing but to the right.


Easy trick to get result of the left shift operation, e.g.

15 << 2 = 15 * (2*2) = 60

15 << 3 = 15 * (2*2*2) = 120

15 << 5 = 15 * (2*2*2*2*2) = 480

and so on..

So it's:

(number on left) multiplied by (number on right) times 2.

Same goes for right shift operator (>>), where:

(number on left) divided by (number on right) times 2


"<<" is a bit-shift left. Please review PHP's bitwise operators. http://php.net/manual/en/language.operators.bitwise.php

A more in-depth explanation:

This means multiply by two because it works on the binary level. For instance, if you have the number 5 in binary

 0101

and you bit-shift left once to (move each bit over one position)

 1010

then your result is 10. Working with binary (from right to left) is 2^0, 2^1, 2^2, 2^3, and so on. You add the corresponding power of two if you see a 1. So our math for our new result looks like this:

 0 + 2^1 + 0 + 2^3
 0 + 2   + 0 + 8 = 10

Tags:

Php

Operators