gpt4 book ai didi

php - 在 PHP 中 2<<3 返回 16 和 3 << 2 返回 12 请有人解释一下吗

转载 作者:行者123 更新时间:2023-12-02 21:44:02 26 4
gpt4 key购买 nike

<?php
echo 2<<3; //Output 16
echo '---';
echo 3<<2; //Output 12
?>

试图找出逻辑。但最终都是徒劳!!有人可以解释一下吗

最佳答案

<<运算符是 bitwise operator 。这基本上意味着数字被视为二进制数,并且交互是关于移动位。

让我们看一下数字和运算:

首先,2 << 3

0b000010 // 2 in binary
0b010000 // move the bits three left, we get 16

然后3 << 2

0b000011 // 3 in binary
0b001100 // move the bits two left, we get 12

从上面链接的手册页:

Shift the bits of $a $b steps to the left (each step means "multiply by two")

所以3<<2实际上意味着3*(2^2) ,而2<<3意味着2*(2^3) .

关于php - 在 PHP 中 2<<3 返回 16 和 3 << 2 返回 12 请有人解释一下吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19855362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com