Share on Google+Share on Google+

PHP Bitwise Operator

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:




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/>";



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




Posted on: March 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.