The PHP bitwise operators are comparing two numbers according to their bits, which mean getting a decimal number of two values according to the binary bits ( 0s, 1s ).

## Table Of Contents

For a quick example.

```
<?php
echo 10 & 11; // output: 10
?>
```

In the previous example, the comparing was for two binaries like this **00001010 **for 10 and **00001011** for 11. Our mission in this tutorial that we are going to cover how to convert the decimal number to the binary number.

Before getting started, you have to look at the below table to know what are the bitwise operators in PHP.

Operator | Name | Example |
---|---|---|

&= | Bitwise And | $a = $x & $y; |

|= | Bitwise Or | $a = $x | $y; |

^= | Bitwise Xor | $a = $x ^ $y; |

<<= | Left Shift | $a = $x << $y; |

>>= | Right Shift | $a = $x >> $y; |

Let’s getting started with bitwise AND operator but before that let’s understand the binary bits system.

## An Introduction to the Binary Number System

The binary bits are representing its values by only two digits ( 0 and 1 ) Zero and One. Which are the machine code.

Anyway, for quick example, check the following image to know what is the binary number.

You know the bits already starting from 1 and every time is going to be a double multiplication with each new number.

So each number (0 or 1) has a size in memory from right to left. The previous image shows you this decimal number (2 + 8 + 64) = **74**.

In the next section, I am going to cover all bitwise operators. Let’s dive right in.

## PHP Bitwise AND Operator

The PHP bitwise AND operator already starting from right to left according to the bit number. So to compare two numbers in binary, you have to follow the below way to get the final decimal value.

Firstly, the bitwise AND operator takes this symbol “&” which refers to the two operands in the stack for example.

```
<?php
$number_1 = 55;
$number_2 = 100;
// The Bitwise AND operator would be written like this
echo $number_1 & $number_2; // => Output: 36
?>
```

So why it gave us 36 in output ?

To answer this question, you have to read more about the binary number system.

Anyway, the first variable $number_1 takes 55 which would be written in the binary like this “00110111”. To understand it, check the following image.

I will use the same way to extract the binary from the decimal number in the $number_2 variable. So it would be written like this, “01100100”.

Now we need to compare the two values together using the bitwise AND operator to extract the 36 as a decimal number, see the following diagram.

So the two operands would have the same binary number (1) which means it has the same two values in comparing such as ( 4 = 4 ) = 1 and ( 32 = 32 ) = 1.

Anyway, in the next section I am going to move to the bitwise OR operator.

## PHP Bitwise OR Operator

The PHP bitwise OR operator already starting from right to left according to the bit number. So to compare two numbers in binary, you have to follow the following way to get the final decimal value.

First the bitwise OR operator takes this symbol “|” which refers to choose one operand of two operands in the stack for example.

```
<?php
$number_1 = 55;
$number_2 = 100;
// The Bitwise OR operator would be written like this
echo $number_1 | $number_2; // => Output: 119
?>
```

The next diagram shows you how to extract the binary from these two decimal numbers and comparing them together.

So in this operator the collecting works according to 0 or 1 bit.

In the following section I will cover the bitwise XOR operator. Lets explain it in-depth.

## PHP Bitwise XOR Operator

The bitwise XOR operator only collecting only 1s and 0s between the two operands.

The XOR operator takes this symbol “^”. For example.

```
<?php
$number_1 = 55;
$number_2 = 100;
// The Bitwise XOR operator would be written like this
echo $number_1 ^ $number_2; // => Output: 83
?>
```

The following image shows you how to extract the binary from the two operands in the bitwise XOR operator.

Anyway, in the following you will understand the left shift operator.

## PHP Bitwise Left Shift Operator

This operator is pushing the bits into left side. The left shift operator can be written with these two symbols “<<“.

So if the left shift is taking 3 digits that means, pushing the bits into left with 3 numbers. For example.

We have 00000010 as a binary number and I need to push it to the left position with 3 numbers.

So it would be like this 00010000. Let’s see an example.

```
<?php
$push = 4;
$number = 10;
// The bitwise left shift operator would be written like this
echo $number << $push; // => Output: 160
?>
```

The result is 160 – Check the below diagram to understand how the bitwise left shift operator works.

In the next section, you will learn the bitwise right shift operator.

## PHP Bitwise Right Shift Operator

This operator is pulling the bits into right side. The right shift operator can be written with these two symbols “>>”.

For example.

```
<?php
$pull = 2;
$number = 10;
// The bitwise right shift operator would be written like this
echo $number >> $pull; // => Output: 2
?>
```

Let’s see the calculation result in the following diagram.

It is pulling the bits to the right.

## Wrapping Up

In this article, you understood what does mean PHP bitwise operator also we focused more in each one with an example. To understand the PHP Integer type you have to read this article.

Thank you for reading. Stay tuned for our next articles.