PHP | Bitwise Operators Last Updated : 05 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then calculation is performed on the operands. The mathematical operations such as addition , subtraction , multiplication etc. can be performed at bit-level for faster processing. In PHP, the operators that works at bit level are: & (Bitwise AND) : This is a binary operator i.e. it works on two operand. Bitwise AND operator in PHP takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Syntax: $First & $Second This will return another number whose bits are set if both the bit of first and second are set. Example: Input: $First = 5, $Second = 3 Output: The bitwise & of both these value will be 1. Explanation: Binary representation of 5 is 0101 and 3 is 0011. Therefore their bitwise & will be 0001 (i.e. set if both first and second have their bit set.) | (Bitwise OR) : This is also binary operator i.e. works on two operand. Bitwise OR operator takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1. Syntax: $First | $Second This will return another number whose bits are set if either the bit of first or second are set. Example: Input: First = 5, Second = 3 Output: The bitwise | of both these value will be 7. Explanation: Binary representation of 5 is 0101 and 3 is 0011. Therefore their bitwise | will be 0111 (i.e. set if either first or second have their bit set.) ^ (Bitwise XOR ) : This is also binary operator i.e. works on two operand. This is also known as Exclusive OR operator. Bitwise XOR takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. Syntax: $First ^ $Second This will return another number whose bits are set if one of the bit in first or second is set but not both. Example: Input: First = 5, Second = 3 Output: The bitwise ^ of both these value will be 6. Explanation: Binary representation of 5 is 0101 and 3 is 0011. Therefore their bitwise ^ will be 0110 (i.e. set if either first or second have their bit set but not both.) ~ (Bitwise NOT) : This is a unary operator i.e. works on only one operand. Bitwise NOT operator takes one number and inverts all bits of it. Syntax: ~$number This will invert all the bits of $number. Example: Input: number = 5 Output: The bitwise '~' of this number will be -6. Explanation: Binary representation of 5 is 0101. Therefore the bitwise ~ of this will be 1010 (inverts all the bits of the input number) << (Bitwise Left Shift) : This is a binary operator i.e. works on two operand. Bitwise Left Shift operator takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Syntax: $First << $Second This will shift the bits of $First towards the left. $Second decides the number of time the bits will be shifted. Example: Input: First = 5, Second = 1 Output: The bitwise << of both these value will be 10. Explanation: Binary representation of 5 is 0101 . Therefore, bitwise << will shift the bits of 5 one times towards the left (i.e. 01010 ) Note: Bitwise left shift with one bit is equivalent to multiplication with 2. >> (Bitwise Right Shift) : This is also binary operator i.e. works on two operand. Bitwise Right Shift operator takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. Syntax: $First >> $Second This will shift the bits of $First towards the right. $Second decides the number of time the bits will be shifted. Example: Input: First = 5, Second = 1 Output: The bitwise >> of both these value will be 2. Explanation: Binary representation of 5 is 0101 . Therefore, bitwise >> will shift the bits of 5 one times towards the right(i.e. 010) Note: Bitwise right shift with one bit is equivalent to division with 2. Below is the implementation of Bitwise Operators in PHP: PHP <?php // PHP code to demonstrate Bitwise Operator. // Bitwise AND $First = 5; $second = 3; $answer = $First & $second; print_r("Bitwise & of 5 and 3 is $answer"); print_r("\n"); // Bitwise OR $answer = $First | $second; print_r("Bitwise | of 5 and 3 is $answer"); print_r("\n"); // Bitwise XOR $answer = $First ^ $second; print_r("Bitwise ^ of 5 and 3 is $answer"); print_r("\n"); // Bitwise NOT $answer = ~$First; print_r("Bitwise ~ of 5 is $answer"); print_r("\n"); // Bitwise Left shift $second = 1; $answer = $First << $second; print_r("5 << 1 will be $answer"); print_r("\n"); // Bitwise Right shift $answer = $First >> $second; print_r("5 >> 1 will be $answer"); print_r("\n"); ?> Output: Bitwise & of 5 and 3 is 1 Bitwise | of 5 and 3 is 7 Bitwise ^ of 5 and 3 is 6 Bitwise ~ of 5 is -6 5 << 1 will be 10 5 >> 1 will be 2 Comment More infoAdvertise with us Next Article PHP 7 | Spaceship Operator S ShivamKD Follow Improve Article Tags : PHP PHP-basics PHP-Operators Similar Reads PHP Operators In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH 8 min read PHP 7 | Spaceship Operator This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by "". This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands. This ope 2 min read 'AND' vs '&&' as operator in PHP 'AND' Operator The AND operator is called logical operator. It returns true if both the operands are true. Example: php <?php // Variable declaration and // initialization $a = 100; $b = 50; // Check two condition using // AND operator if ($a == 100 and $b == 10) echo "True"; else echo 3 min read PHP | gmp_or() Function The gmp_or() is an inbuilt function in PHP which is used to calculate the bitwise OR of two GMP numbers(GNU Multiple Precision : For large numbers). Syntax: gmp_or($num1, $num2) Parameters: This function accepts two GMP numbers, $num1, $num2 as mandatory parameters as shown in the above syntax. Thes 2 min read PHP decbin( ) Function While working with numbers, many times we need to convert the bases of number and one of the most frequent used conversion is decimal to binary conversion. PHP provides us with a built-in function, decbin() for this purpose.The decbin() function in PHP is used to return a string containing a binary 1 min read PHP | boolval() Function The boolval() function is an inbuilt function in PHP which gives the Boolean value for a given expression. Syntax: boolean boolval( $expr ) Parameter: This function accepts only one parameter as shown in above syntax and described below: $expr: The expression or the scalar which you want to change i 3 min read Like