PHP << and >> (bitwise operators Shift Left and Shift Right) Introduction

If your are new to PHP and stumble upon the << and >> operators they are not similar to the << and >> operators in C++. This guide attempts to explain the concept behind shift left and shift right – what << and >> do.

Lets say we have a variable called $a. If we were to shift it 5 times to the left it would be like multiplying it by 2 five times;  $a * 2 * 2 * 2* 2 * 2. Now since you love math you know that is $a * 25

So when you see the syntax $a << 5, you know that is $a * 25;

$a << 5 = $a * 25

Lets say we have $a >> 5. In this case you can see that the we want to shift right. A great guess we be that what this operation does is it divides $a by 2 five times, $a /2 / 2 / 2 / 2 / 2, or $a / (25)

$a >> 5 = $a / 25

You might notice I purposefully chose 5 because I originally titled this tutorial as “PHP << and >> (bit-wise operators Shift Left and Shift Right) Explained for a five year old” – jokingly suggesting a five year old can handle it, maybe, I dunno, I have seen some smart kids. I also thought it would be easier on the eyes than seeing $a and $b everywhere.

Some history;

The shift operators came from the C language designed by  Dennis Ritchie. When C was designed it was running very close to the hard ware, in fact shift is an instruction from Assembly language (SHL and SHR).  This is due to the way the value is stored in memory, it’s stored in binary. A binary representation of the value, if literally moved over, will have the result (since binary is numbering system based on twos) of being divided by two.

Side note: if you don’t know binary go learn it real quick, if you do know binary, test how well you know it here

For example, 80 written in binary is 1010000. If I shift is right once I get 0101000, which is 40. If I shift it again (80 >>2) we get 0010100 which is 20. As you can see shift right and shift left are the opposites of each other, so if I shift it right five times then shift it left five times, it will cancel out.

The shift operation are not mathematically associative, that is if we do the linear order in a different fashion, we will get the same result. So if we have ($a << $b) << $c, we are not guaranteed to get the same result as $a << ($b << $c).

($a << $b) << $c != $a << ($b << $c)

The shift operation is not commutative either. So if we have $a << $b and we switch the terms around, $b << $a, we will get a different result

$a << $b != $b << $a

You are probably now wondering when  would you use this operator? Well any time you start writing code that performs a lot of integer multiplication, so for people with careers specializing in file compression, cryptography, audio and video processing, mathematics, Wall Street, accounting, banking, the list goes on.

Here is a program that performs multiplication using only bit-shifts and addition

<?PHP
$c = 0;
while($b != 0) {
     if(($b & 1) != 0) {
          $c += $a;  //also same as $c = $c + $a;
      }
      $d = $a << 1;
      $e = $b >> 1;
}
print $c;
?>

It was Aliens - they gave us the knowledge!

In fact, the Egyptians had a similar method for performing multiplication; mandatory wikipedia link.

 So now I have a question for you, please provide what you think is the answer in the comments section!

What will be printed to the screen when the below code is ran?

HINT: Wait until next month when I talk about bitwise ANDs and ORs  (‘&’ and ‘|’ in PHP)
<?php
    $a = (1 << 0 );
    $b = (1 << 1);
    echo ($b | $a) << 2;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>