Search code examples
phpoperators

PHP Operator <<


What does the << Operator mean in php?

Example:

$t = 5;
$foo = 1 << ($t);
echo($foo); 

echo produces: 32


Solution

  • 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.