Bitwise operators
Here is a list of all the bitwise operators. As it will only work with valid integers you can, just to be sure, either use typecasting or put it in the intval() function. Example:
PHP Example: (!)<?php
// using typecasting
$c = (int)$a & (int)$b;
// using intval()
$c = intval($a) & intval($b);
?>
Also note that the bitwise operators should not be confused with the logical operators.
A list of the bitwise operators Example: Name: Result:
$a & $b AND Bits that are set in both $a and $b are set.
$a | $b OR Bits that are set in either $a or $b are set.
Source:
http://www.phpfreaks.com/tutorials/151/5.php
$a ^ $b XOR Bits that are set in $a or $b, but not both are set.
~ $a NOT Bits that are not in $a set are set, and vice versa.
$a >> $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a << $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")
Now follows examples and explanations of the different types of bitwise operators.
AND
AND takes two decimal versions of binary values and return a new decimal value. Here is an example:
PHP Example: (!)<?php
$a = 20;
$b = 36;
echo $a & $b; // outputs: 4
?>
Now why does this output 4? Let's try to put it in a table:
20 & 36 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
$b 0 0 1 0 0 1 0 0 = 57
New value 0 0 0 0 0 1 0 0 = 4
As you see, the only bit they share is 4, so that is the value returned. Pretty simple, eh?
OR
OR also takes two decimal versions of binary values and return a new decimal representation of a binary value. Example:
PHP Example: (!)<?php
$a = 20;
$b = 36;
echo $a | $b; // outputs: 52
?>
20 | 36 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
$b 0 0 1 0 0 1 0 0 = 57
New value 0 0 1 1 0 1 0 0 = 52
OR takes bits that are either set in $a or $b and sets them all in the returned value. That results in bit 4, 16 and 32 being set which then results in 52.
XOR
XOR is like OR, only that it is the ones which are exclusively set. Example (same numbers as before):
PHP Example: (!)<?php
$a = 20;
$b = 36;
echo $a ^ $b; // outputs: 48
?>
20 ^ 36 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
$b 0 0 1 0 0 1 0 0 = 57
New value 0 0 1 1 0 0 0 0 = 48
As you can see, 4 is set on both, so it will not be set, but 32 is set in $b and 16 is set in $a (and no other) which means that both of them will be set, and as 16+32=48 it will return 48.
NOT
NOT turns sets on bits off and off bits on. You could say that it reverses the bit. NOT is always used with some other operator. Example:
PHP Example: (!)<?php
$a = 20;
$b = 36;
echo ~$a & $b; // outputs: 32
?>
This finds out what bits that are NOT set in $a and what bits that are set in $b.
~20 & 36 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
$b 0 0 1 0 0 1 0 0 = 57
New value 0 0 0 1 0 0 0 0 = 32
The above one might be a little tricky. First it reverses 20 (00010100) which results in 11101011, then the AND operator is used on it and the only "shared" bit is 32 which is in both ~$a and $b.
Note that you don't have to use AND, you can use all the other ones as well.
Left shift
Left shift shifts all bits X positions left (one shift is also equal to multiply with 2). Example:
PHP Example: (!)<?php
$a = 20;
echo $a << 1; // outputs: 40
?>
20 << 1 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
New value 0 0 1 0 1 0 0 0 = 40
As you see all the on bits are moved to the 1 to the left, also resulting in a multiplication by 2. New value = 40.
Right shift
Right shift works the same way except that the bits are shifted right instead of left (also equal to divide by 2). Example:
PHP Example: (!)<?php
$a = 20;
echo $a >> 1; // outputs: 10
?>
20 >> 1 128 64 32 16 8 4 2 1
$a 0 0 0 1 0 1 0 0 = 20
New value 0 0 0 0 1 0 1 0 = 40
As you see all the on bits are moved to the 1 to the right, also resulting in a division by 2. New value = 10.
Implementation and usage
One thing you can use this for could be permissions systems. Imagine a forum system with the following permission flags: can login (is not banned), can start topic, can reply to topic, is mod, is admin:
PHP Example: (!)<?php
// First we define our permission flags:
define('CAN_LOGIN', 1);
define('CAN_START_TOPIC', 2);
define('CAN_REPLY', 4);
define('IS_MOD', ;
define('IS_ADMIN', 16);
// We have three users: John who is just regular user who may login, post and reply, George who is a moderator as well, and Jim who is banned (as he does not have the CAN_LOGIN flag):
$john = 7; // CAN_LOGIN | CAN_START_TOPIC | CAN_REPLY
$george = 15; // CAN_LOGIN | CAN_START_TOPIC | CAN_REPLY | IS_MOD
$jim = 0; // *nothing*
// We are at the login screen. All three users are trying to login. This checks if the bits set for them shares the bits set in CAN_LOGIN:
if($john & CAN_LOGIN)
{
echo "John can loginn";
}
else {
echo "John cannot loginn";
}
if($george & CAN_LOGIN)
{
echo "George can loginn";
}
else {
echo "George cannot loginn";
}
if($jim & CAN_LOGIN)
{
echo "Jim can loginn";
}
else {
echo "Jim cannot loginn";
}
// Now both John and George are trying to trying to enter a special control panel for administrators only:
if($john & IS_ADMIN)
{
echo "John can enter admin control paneln";
}
else {
echo "John cannot enter admin control paneln";
}
if($george & IS_ADMIN)
{
echo "George can enter admin control paneln";
}
else {
echo "George cannot enter admin control paneln";
}
// George was supposed to recently being promoted to administrator, but his manager apparently forgot to grant him new permissions. George calls him and the manager updates George's permissions:
$george = $george | IS_ADMIN;
// George tries again:
if($george & IS_ADMIN)
{
echo "George can enter admin control paneln";
}
else {
echo "George cannot enter admin control paneln";
}
// John creates a new topic:
if($john & CAN_START_TOPIC)
{
echo "John can start a new topicn";
}
else {
echo "John cannot start a new topicn";
}
// George thinks John is starting all sorts of wierd topics, so he decides (with his new admin powers) that John's CAN_START_TOPIC permission flag should be revoked:
$john = $john & ~CAN_START_TOPIC ;
// Now John tries to start a new topic again:
if($john & CAN_START_TOPIC)
{
echo "John can start a new topicn";
}
else {
echo "John cannot start a new topicn";
}
/* This script will output:
John can login
George can login
Jim cannot login
John cannot enter admin control panel
George cannot enter admin control panel
George can enter admin control panel
John can start a new topic
John cannot start a new topic
*/
?>
Edit: There is apparently some display error with the code. There is supposed to be a linefeed at the end of each echo, but this is just a minor issue that won't really have significant effect.