I've been testing bitwise operators in PHP and ran into a very strange problem. The following procedures
<?php $foon = b'11111111'; for ($i = 0; $i <= 7 ; $i++) { $foo = (($foon & (1 << $i)) > 0) ? 1 : 0; echo "目标的第" . $i . "位是" . $foo . "<br />" . PHP_EOL; }
should produce the following output
目标的第0位是1 目标的第1位是1 目标的第2位是1 目标的第3位是1 目标的第4位是1 目标的第5位是1 目标的第6位是1 目标的第7位是1
However, the actual output is
目标的第0位是1 目标的第1位是1 目标的第2位是1 目标的第3位是0 目标的第4位是0 目标的第5位是0 目标的第6位是1 目标的第7位是1
Am I overlooking something very obvious, or is there a bug in PHP? I'm using PHP version 8.2.4 installed via XAMPP. Thank you so much.
It doesn’t matter, I figured it out. b'11111111' is not actually a binary representation in PHP, although it does not raise an error. Assigning 0b11111111 to my variable gives me the correct answer.