Alex Rivera | Logout

PHP Operator <<

Asked 2010-12-19T22:50:57.357
28

What does the << Operator mean in php?

Example:

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

echo produces: 32

Edit
Report

1 Answer

8

"<<" 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
answered 2010-12-19T22:53:26.933

Your Answer