What does >> mean in PHP?

后端 未结 8 1866
天涯浪人
天涯浪人 2021-02-19 05:13

Consider:

echo 50 >> 4;

Output:

3

Why does it output 3?

8条回答
  •  误落风尘
    2021-02-19 06:06

    The >> operator is called a binary right shift operator.

    Shifting bits to the right 4 times is the same as dividing by two, four times in a row. The result, in this case would be 3.125. Since 50 is an int, bit shifting will return the floor of this, which is 3.

    Put another way, 50 is 0b110010 in binary. Shifted 4 times we have 0b11, which is 3 in decimal.

提交回复
热议问题