PHP |位运算符

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

null
  • &(按位和): 这是一个二进制运算符,即它在两个操作数上工作。PHP中的按位AND运算符将两个数字作为操作数,并对两个数字的每一位进行AND运算。只有当两位都为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.)
    
  • ~(按位不) :这是一个一元运算符,即只作用于一个操作数。按位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中按位运算符的实现:

<?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( "" );
// Bitwise OR
$answer = $First | $second ;
print_r( "Bitwise | of 5 and 3 is $answer" );
print_r( "" );
// Bitwise XOR
$answer = $First ^ $second ;
print_r( "Bitwise ^ of 5 and 3 is $answer" );
print_r( "" );
// Bitwise NOT
$answer = ~ $First ;
print_r( "Bitwise ~ of 5 is $answer" );
print_r( "" );
// Bitwise Left shift
$second = 1;
$answer = $First << $second ;
print_r( "5 << 1 will be $answer" );
print_r( "" );
// Bitwise Right shift
$answer = $First >> $second ;
print_r( "5 >> 1 will be $answer" );
print_r( "" );
?>


输出:

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
© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享