Bitwise Operators in PHP

The bitwise operators are those operators in PHP which allow you to perform operations on the binary representations of integers. Each integer value is represented by a sequence of bits (0s and 1s).

We commonly use bitwise operators to perform operations on the operands at bit-level. Before the operation, the operands are converted to their binary (bit-level) representations, and then the bitwise calculation is performed.

You can perform mathematical operations such as addition, subtraction, multiplication, etc. at the bit-level for faster processing.

Note that you can use the PHP bitwise operators only for integer types. If you attempt to apply bitwise operators to non-integer types, such as float, it will either result in an error or produce unintended behavior.

PHP Bitwise Operators


There are mainly six types of bitwise operators available in PHP language. They are listed in the below.

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Bitwise Shift Left (<<)
  • Bitwise Shift Right (>>)

Bitwise AND Operator

The bitwise AND operator is used to perform the bitwise AND operation between two integral operands. It is represented by a symbol (&) which is called ampersand. This operator compares two integer numbers bit by bit. If both corresponding bits are 1, the resulting bit is 1 in each bit position. Otherwise, it’s 0.

Example 1:

<?php
$a = 6; // 110 in binary form.
$b = 3; // 011 in binary form.
$result = $a & $b; // 010 (2 in decimal form.)
echo $result;
?>
Output:
       2

In this example, the binary representation of 6 and 3 is 110 and 011, respectively. The AND operation results in 010, which is 2 in the decimal number system.

Bitwise OR Operator

The bitwise OR operator is used to perform an OR operation on bits of integral numbers. It is represented by a symbol (|) called pipe symbol. In OR operation, each bit of the first operand (number) is compared with the corresponding bit of the second operand. If one or both of the corresponding bits is 1, the resulting bit is 1. If both bits are 0, the result is 0.

Example 2:

<?php
$a = 6; // 110 in binary
$b = 3; // 011 in binary
$result = $a | $b; // 111 (7 in decimal)
echo $result;
?>
Output:
      7

The binary representation of integer numbers 6 and 3 results in 111 after the OR operation, which equals 7 in the decimal number system.

Bitwise XOR Operator

This bitwise exclusive OR operator performs exclusive OR (XOR) operation on the bits of numbers. It is represented by a symbol ^ called cap. In the XOR operation, each bit of the first operand (number) is compared with the corresponding bit of the second operand. If the corresponding bits are different, the resulting bit is 1. Otherwise, it’s 0 if both bits are 0 or both bits are 1.

Example 3:

<?php
$a = 6; // 110 in binary
$b = 3; // 011 in binary
$result = $a ^ $b; // 101 (5 in decimal)
echo $result;
?>
Output:
     5

Bitwise NOT Operator

The bitwise NOT operator in PHP is basically an inverter which inverts (or flips) the bits of its operand. It is represented by the symbol (~).

The bitwise NOT operator returns the reverse of its operand or value. It converts all 1s to 0s, and all the 0s to 1s. Therefore, it is also known as unary operator or bit flip or one’s complement operator.

Example 4:

<?php
$a = 6; // 110 in binary
$result = ~$a;
echo $result;
?>
Output:
      -7

In this example, the NOT operator inverts the bits of 6. Since PHP stores integers using 32 bits, the result is a negative number.

Bitwise Shift Left Operator

The bitwise shift left operator (<<) shifts the bits of its operand to the left by a specified number given as the right operand. It is basically a rotation operator, which means that leftmost bit will wrap around to the left. Each left shift doubles the number.

Example 5:

<?php
$a = 5; // 101 in binary
$result = $a << 1; // 1010 (10 in decimal number system)
echo $result;
?>
Output:
      10

In this example, the binary representation of number 5 is 101. When this binary 101 is shifted to left by one position, the result is 1010 which is 10 in decimal number system.

Bitwise Shift Right (>>) Operator

The bitwise shift right operator shifts the bits of its operand to the right by a specified number given as the right operand. It shifts numbers to the right. The rightmost bits are removed. Each right shift halves the number.

Example 6:

<?php
$a = 8; // 1000 in binary
$result = $a >> 2;
echo $result;
?>
Output:
       2

In this example, the binary representation of the number 8 is 1000. When this binary value 1000 is shifted two positions to the right using the bitwise right shift operator (>>), it becomes 0010, which is equivalent to 2 in the decimal number system.

Basic Examples of Bitwise Operations


Let’s take some simple examples of how bitwise operators work:

Example 7: Checking a number is odd or even

<?php
$number = 5;
if ($number & 1) {
     echo "Odd";
} else {
     echo "Even";
}
?>
Output:
      Odd

Example 8: Swapping two numbers without a temporary variable

<?php
$a = 10;
$b = 15;

$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;

echo "a = $a, b = $b";
?>
Output:
      a = 15, b = 10

In this tutorial, you have learned about all six bitwise operators available in PHP with the help of examples. I hope you have understood the basic concepts of bitwise operators and practiced the examples provided.