PHP | Bitwise Operators


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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");
?>

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.