📜  PHP |位运算符

📅  最后修改于: 2022-05-13 01:56:33.043000             🧑  作者: Mango

PHP |位运算符


位运算符用于对操作数执行位级操作。运算符首先转换为位级,然后对操作数进行计算。诸如加法、减法、乘法等数学运算可以在位级别执行以加快处理速度。在PHP中,位级别的运算符有:

  • &(按位与):这是一个二元运算符,即它适用于两个操作数。 PHP中的位与运算符将两个数字作为操作数,并对两个数字的每一位进行与运算。仅当两个位都为 1 时,AND 的结果才为 1。

    语法

    $First & $Second
    
    This will return another number whose bits are 
    set if both the bit of first and second are set.
    

    例子:

    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.)
    
  • | (按位或):这也是二元运算符,即适用于两个操作数。按位或运算符将两个数字作为操作数,并对两个数字的每一位进行 OR。 OR 的结果为 1 两位中的任何一位为 1。

    语法

    $First | $Second
    
    This will return another number whose bits are 
    set if either the bit of first or second are set.
    

    例子:

    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.)
    
  • ^(按位异或):这也是二元运算符,即适用于两个操作数。这也称为异或运算符。按位异或将两个数字作为操作数,并对两个数字的每一位进行异或。如果两个位不同,则异或的结果为 1。

    语法

    $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.
    

    例子:

    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) : 这是一个一元运算运算符,即只对一个操作数起作用。按位非运算符取一个数字并将其所有位取反。

    语法

    ~$number
    
    This will invert all the bits of $number.
    

    例子:

    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)
    
  • <<(按位左移):这是一个二元运算符,即适用于两个操作数。按位左移运算符取两个数字,左移第一个操作数的位,第二个操作数决定要移位的位数。

    语法

    $First << $Second
    
    This will shift the bits of $First towards the 
    left. $Second decides the number of time the
    bits will be shifted.
    

    例子:

    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 )
    

    注意:按位左移一位等价于乘以 2。

  • >>(按位右移):这也是二元运算符,即适用于两个操作数。按位右移运算符取两个数字,右移第一个操作数的位,第二个操作数决定要移位的位数。

    语法

    $First >> $Second
    
    This will shift the bits of $First towards the 
    right. $Second decides the number of time the 
    bits will be shifted.
    

    例子:

    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)
    

    注意:按位右移一位相当于除以 2。

下面是PHP中按位运算符的实现:

> $second;
    print_r("5 >> 1 will be $answer");
      
    print_r("\n");
?>

输出:

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