Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Operator <<

Tags:

operators

php

What does the << Operator mean in php?

Example:

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

echo produces: 32

like image 919
Petrogad Avatar asked Dec 19 '10 22:12

Petrogad


People also ask

What is the << operator used for?

The left shift operator ( << ) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

What does >> mean in PHP?

The >> operator is called a binary right shift operator. Shifting bits to the right 4 times is the same as dividing by two, four times in a row. The result, in this case would be 3.125 . Since 50 is an int, bit shifting will return the floor of this, which is 3 .

What is << In bitwise?

The bitwise shift operators are the right-shift operator ( >> ), which moves the bits of an integer or enumeration type expression to the right, and the left-shift operator ( << ), which moves the bits to the left.


1 Answers

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.

like image 75
Cameron Skinner Avatar answered Oct 09 '22 03:10

Cameron Skinner