【问题标题】:The binary in php, why is 63 bits?php中的二进制,为什么是63位?
【发布时间】:2019-04-30 03:47:39
【问题描述】:

我使用的系统和PHP版本:

  • 操作系统:Centos 7 x86_64
  • PHP:PHP 7.2

网址:http://php.net/manual/en/function.decbin.php

代码:

<?php

$a = PHP_INT_SIZE;
$b = PHP_INT_MAX;
$c = PHP_INT_MIN;

echo "The value of \$a: ", $a . "\n";
echo "The value of \$b: ", $b . "\n";
echo "The value of \$c: ", $c . "\n\n";

echo "The binary of \$b: " . decbin($b) . "\n";
echo "The binary of \$c: " . decbin($c) . "\n";

输出:

The value of $a: 8
The value of $b: 9223372036854775807
The value of $c: -9223372036854775808

The binary of $b: 111111111111111111111111111111111111111111111111111111111111111
The binary of $c: 1000000000000000000000000000000000000000000000000000000000000000

问题:

  • 二进制数最小值为64
  • 最大的二进制数是63,为什么?

感谢您的回答。

【问题讨论】:

    标签: php binary


    【解决方案1】:

    因为$b 中最左边的位是 0 并且没有被打印出来。

    尝试打印decbin($a)(因为$a 是8),看看它不会打印为64 位,只有4。

    如果要显示最左边的 0,请使用sprintf 格式化字符串,如

    echo "The binary of \$b: " . sprintf("%064b", decbin($b)) . "\n";
    

    将格式字符串中的64 替换为您想要显示的任意多位。

    【讨论】:

    • 还有一个 0 因为二进制补码表示。
    • The binary of $a: 1000。我明白。感谢您的帮助。
    猜你喜欢
    • 2011-01-31
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2019-07-24
    • 2011-01-29
    • 2011-10-14
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多