0

I found << in a php script and I want to know how it works;

eg:

echo 2 << 4;

it prints 32

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
  • [It is a binary left shift.](http://php.net/manual/en/language.operators.bitwise.php) – Pow-Ian Apr 15 '13 at 14:20
  • 1
    Did you try looking it up [in the manual](http://www.php.net/manual/en/language.operators.bitwise.php)? – Spudley Apr 15 '13 at 14:21
  • `<<` is a bitshift operator in c++, same in php, 2 moved 4 bits left = 32. In theory a bitshift is a very cheap operation to carry out, as it is a bitwise manipulation as opposed to multiplying for example which is an instruction which has to compute an answer. With a bitshift the answer is implicit in the original value. From a programming point of view I always prefer `2 << 4` to `2*2^4` as `2<<4` is much much faster than the alternative. (NB I have not tested the speed of each on php) – GMasucci Apr 15 '13 at 14:31

1 Answers1

2

Shift left

From the php.net manual on Bitwise Operators:

Shift the bits of $a $b steps to the left (each step means "multiply by two")


2 << 4 means 2 * 2^4 = 32

Ragnar123
  • 5,174
  • 4
  • 24
  • 34