Introduction to Bitwise Operators in PHP
As the name suggests, the bitwise operators in PHP are used for performing operations at a bit level on the operands they are to be operated upon. It is done by first converting these operands into their bit-level and afterward the required calculation is done on them. Several mathematical operations can be performed at this bit level rather than the Boolean value level for processing fast.
Top Bitwise Operators in PHP
Some of the bitwise operators in PHP are below:
1. Bitwise AND( & )
Binary operators work on 2 operands. In PHP, Bitwise AND operator takes two numbers as input operands and performs AND on each bit of these two numbers. The result will be boolean and 1 if both the bits are 1 and 0 if any other case.
$first_op & $sec_op
& is the symbol used for performing this operation.
echo $a & $b;
Below is the binary representation of 61 and 32 in the table. As per the AND symbol, it gives the output of 1 only if both bits contain 1 and return 0 in any other case. So as shown below, only the 3rd bit matches the condition and hence the final output is 32.
2. Bitwise OR( | )
As similar to Binary AND, bitwise OR operator takes two numbers as input operands and performs OR on each bit of these two numbers and the result is a Boolean. It returns 1 if either of the bits or both the bits are 1. Hence the result will be 0 only if both digits are 0.
$first_op | $sec_op
| is the symbolical representation of bitwise OR.
echo $a | $b;
Below is the binary representation of 50 and 36 respectively as shown in the table. As per the OR operation, we can see that in the 2nd, 3rd, 5th and 6th bit there are digits which are 1 hence the respective position below will also be 1 and the remaining digits are filled by 0 since they do not satisfy the condition. Hence the final output we get is 54.
3. Bitwise XOR( ^ )
This is a binary operator which takes the input of two numbers as operands and performs XOR operation on its every bit and the result of these two numbers will be true if either of the bits is true and the output will be false if both bits are true and both bits are false. The below table can be used for reference to the same.
$first_op ^ $sec_op
^ is the symbolical representation of bitwise XOR.
echo $a ^ $b;
Below is the binary representation of 22 and 31 respectively shown in the table. In the below table, we can see that in the 5th and 8th bits one of the bit is 1 hence in the output those bits are 1 and remaining are 0. The resulting output is 9 when converted to decimal.
4. Bitwise NOT( ~ )
Unlike above all operators, this is a unary operator hence it performs a bitwise NOT on a single operand taken as its input. As the name suggests, bitwise NOT output is the exact opposite of its input.
~ is used to represent bitwise NOT.
echo $a & ~ $b;
In this above code we are first performing bitwise NOT on the second operator and then combining it with a bitwise AND with the first operator to get the output. In the below table we can see the result after NOT is performed on $y and the final output which is 20 in decimal.
5. Bit Shifting
This operation is a bit different from the above operations and involves shifting of bits. There are 2 types of shifting which can be performed: left shift and right shift.
Left Shift( << )
Here the input bits are shifted to their left by the number of places as specified.
4.5 (3,251 ratings)
$first_op << $n
where $n refers to the number of places by which bits must be shifted.
echo $a << $b;
Here we are specifying to shift binary 4 by 3 digits to its left. Hence the resulting output we get is 32.
Right Shift( >> )
Here we are shifting the input bits to their right by the number of places as specified.
$first_op >> $n
where $n is the number of places it is to be shifted.
echo $a >> $b;
Here we are shifting binary 7 by two bits to its right. Hence the resulting output we get is 1.
Above we have covered all the basic bitwise operations that are used in PHP. As their name suggests, they can operate only on each bit of its input and not on whole numbers. They are still used in modern coding as they have a few advantages over the present join since data storing and fetching is relatively lesser and hence boosts the performance too.
This is a guide to Bitwise Operators in PHP. Here we discuss the different bitwise operators in PHP with examples and syntax. You can also go through our other related articles to learn more –