What does >> mean in PHP?

Consider:

echo 50 >> 4;

Output:

3

Why does it output 3?


50 in binary is 11 0010 , shift right by 4 yields 11 which is equal to 3.

See PHP documentation and Wikipedia.


As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right:

$a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two")

50 in binary is 110010 , and the >> operator shifts those bits over 4 places in your example code. Although this happens in a single operation, you could think of it in multiple steps like this:

  • Step 1 - 00011001
  • Step 2 - 00001100
  • Step 3 - 00000110
  • Step 4 - 00000011
  • Since binary 11 is equal to 3 in decimal, the code outputs 3.


    算术右移。

    链接地址: http://www.djcxy.com/p/1778.html

    上一篇: 在PHP中奇怪的打印行为?

    下一篇: >>在PHP中意味着什么?