Bitwise operations in PHP including examples

After I have read some article about the handling of critical errors in PHP, I noticed that the error codes were customized specially for the bitwise operations in PHP, however, in the article’s examples and the comments are used regular operators for comparison in order to check the error codes.

For example, there were such variations:

if ($error['type'] == E_ERROR || $error['type'] == E_PARSE || $error['type'] == E_COMPILE_ERROR){…}

if(in_array($error['type'], array(E_ERROR, E_PARSE, E_COMPILE_ERROR)) {…}
Thereby, I decided to write a short article about the bitwise operations with examples of their use.

Here is some theory

The number in binary system of calculation is a set of zeros and ones. The right bit is zero, and it is called the least significant bit.
The number 5 will appear as 00000101.

There are 6 bitwise operators:

$a & $b - And - Bits that are set in both $a and $b are set.
$a | $b - Or (inclusive or) - Bits that are set in either $a or $b are set.
$a ^ $b - Xor (exclusive or) - Bits that are set in $a or $b but not both are set.
~ $a – Not - Bits that are set in $a are not 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").

Practice with examples

Let us take a look at an example: here is a simple distinction system of access rights to the website.
Here will be available the following access rights: Read, Create, Edit, and Delete. Namely, they are only 4 numbers that can be represented as a 4-bit number in which 1 - means that the user has this access right, and 0 – means no right is permitted.

Let us define the access rights for the following constants:

define('U_READ', 1 << 0); // 0001
define('U_CREATE', 1 << 1); // 0010
define('U_EDIT', 1 << 2); // 0100
define('U_DELETE', 1 << 3); // 1000
define('U_ALL', U_READ | U_CREATE | U_EDIT | U_DELETE); // 1111

Setting the bit values

Let us assign one right.

$user_perm = U_READ; // read only access
Let us use a bitwise operator OR in order to combine multiple rights.

$user_perm = U_READ | U_DELETE; // read and delete
Let us add all rights using this constant.

$user_perm = U_ALL; // all right
If you want to give all the rights except for one or more, you can use the following operators.

$user_perm = U_ALL ^ U_DELETE; // all rights except delete
$user_perm = U_ALL & ~ U_DELETE; // all rights except delete, in this case 2 operators

The difference between these variations is that in the first case a bit is just switched, if it was 1, then it becomes 0, and vice versa. The second variation makes the bit equal to 0, regardless of its actual value.

Checking the bit value

Let us use the bitwise operator AND in order to check the access rights.

if($user_perm & U_READ) // is there a right to read?
It is possible to check the values of several bits, for example:

if($user_perm & ( U_READ | U_DELETE )) // is there a right to read and/or delete

Resetting the bit

If there is a need to remove any right of access, so it should be written:

$user_perm &= ~ U_DELETE; // no delete


If you want to go back to error checking, here are some examples that could be used:

if($error['type'] & ( E_ERROR | E_PARSE | E_COMPILE_ERROR )) {…}
Here are a couple of links concerning the theory:
PHP Documentation
BumBum 2 february 2012, 21:20
Vote for this post
Bring it to the Main Page


0 classified February 15, 2012, 6:09
Useful information .. I am very happy to read this article . Fantastic example and the coding. I really Appreciate this post.

Leave a Reply

Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute