Home Tutorial Php Phpbasics Tutorial PHP Bitwise Operator

 
 

PHP Bitwise Operator
Posted on: March 20, 2010 at 12:00 AM
Advertisement
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

 

Advertisement

Related Tags for PHP Bitwise Operator:


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.

Posted on: March 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com