Home Tutorial Php Phpbasics Tutorial PHP Bitwise Operator

 
 

PHP Bitwise Operator
Posted on: March 20, 2010 at 12:00 AM
In this tutorial we will study about bitwise operator, bitwise operator works on bit or binary number of an integer value. Examples in this tutorial will make it more clear.

Learn PHP Bitwise Operator:

It is another kind of operator ,called bitwise operator and  supported by PHP. It is so called because instead of operating on the integer value, it works on the binary number, like if we want to perform the following task,

12>>2, then :

0000 1100 >> 2:

After first shift, 0000 0110 and after second shift, 0000 0011

Output would be 3 and from the above example it is very clear that after every shift 1 is moved towards right side and the blank space is filled by 0.

So, from the above example we can see that the right shift operator operates on binary number, that's why it is known as bitwise operator.

 

Example of PHP Bitwise Operator:

<?php

$a=10;

$b=2;

echo "\$a & \$b = ".($a & $b)."<br/>";

echo "\$a | \$b = ".($a | $b)."<br/>";

echo "\$a ^ \$b = ".($a ^ $b)."<br/>";

echo "~(\$a) = ".~$a."<br/>";

echo "\$a >> \$b = ".($a >> $b)."<br/>";

echo "\$a << \$b = ".($a << $b)."<br/>";

?>

Output:

$a & $b = 2
$a | $b = 10
$a ^ $b = 8
~($a) = -11
$a >> $b = 2
$a << $b = 40

 

Related Tags for PHP Bitwise Operator:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.