The Wayback Machine - https://web.archive.org/web/20240706020134/https://www.geeksforgeeks.org/php-bitwise-operators/
Open In App

PHP | Bitwise Operators

Last Updated : 05 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
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 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


Previous Article
Next Article

Similar Reads

JavaScript Bitwise Operators
JavaScript uses 32-bit Bitwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number. Bitwise Operat
4 min read
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Equal Operator == The comparison operator called Equal Operator is the double equal sign "==". This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same. This should always be kept in mind that the
2 min read
Difference between the (=), (==), and (===) operators in PHP
In PHP, the '=' operator is used for assignment, while the '==' operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the '===' operator is used for strict equality comparison, meaning it checks if two values are equal and of the same data type. = OperatorT
3 min read
PHP Operators
In this article, we will see how to use the operators in PHP, &amp; various available operators along with understanding their implementation through the examples. Operators are used to performing operations on some values. In other words, we can describe operators as something that takes some values, performs some operation on them, and gives a re
10 min read
HTML5 | Mathematical operators
Mathematical Operators are used for representation of mathematical and technical operators. In HTML5 some special codes are used. For the representation of these operators we can use the name or hexadecimal(Hex) reference. Examples: Input : &amp;minus or &amp;#8722 Output : - Input : &amp;times or &amp;#215 Output : x Input : &amp;frac34 or &amp;#1
1 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &amp;&amp;:(AND) Evaluates operands and retu
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
SASS | Booleans and Boolean operators
SASS supports boolean values: true false It is used in conditional compilation where the expression evaluates to either of these values. Assign a boolean value to the variable: $variable: true; or $variable: false; Usage in Conditional Compilation: We can use boolean values in conditional compilation. See the example below, we have passed a true va
2 min read
ES6 Operators
An expression is a special kind of statement that evaluates to a value. Every expression consists of Operands: Represents the data.Operator: which performs certain operations on operands. Consider the following expression - 2 / 3, in the expression, 2 and 3 are operands and the symbol /is the operator.JavaScript supports the following types of oper
7 min read
SASS | Operators
SASS provides and supports a variety of useful operators in order to work with different values. These operators consist of the standard mathematical operators like +, -, /, and *, and apart from them operators of various other types listed below: +, -, *, /, and % have their usual meaning of mathematics for the numbers with special behavior for so
2 min read
three90RightbarBannerImg