PHP var 变为负值
我有这段代码:
<?php
$integer = 33963677451;
$integer &= 0xFFFFFFFF;
echo $integer;
?>
但是,当我执行它时,输出是
-396060917
The same function in Python
if __name__ == '__main__':
integer = 33963677451
integer &= 0xFFFFFFFF
print integer
The output 是
3898906379
It似乎PHP不能存储大变量。你们知道如何在 PHP 中得到与 Python 中相同的结果吗?
谢谢 !
I have this code :
<?php
$integer = 33963677451;
$integer &= 0xFFFFFFFF;
echo $integer;
?>
But, when I execute it, the output is
-396060917
The same function in Python
if __name__ == '__main__':
integer = 33963677451
integer &= 0xFFFFFFFF
print integer
The output is
3898906379
It seems PHP can't store big variables. Do you guys have any idea how to get the same result in PHP I have in Python ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
该值对于整数来说太大了,python 动态地将其转换为 long 变量。 INT_MAX 定义了整数在转换为双精度数之前可以有多大。如果您想在没有怪癖的情况下操作大数,请使用 GMPLIB 多精度算术库。
That value is too big for an integer, python dynamically casts that into a long variable. INT_MAX defines how big your integer can be before it switches over into a double. If you want to manipulate large numbers without quirks use GMPLIB a multiple precision arithmetic library.
您的
33,963,677,451
高于有符号 32 位整数支持的范围(最大 2,147,483,647)。 64 位 PHP 版本确实支持更高的值。Your
33,963,677,451
is above what a signed 32bit integer supports (max 2,147,483,647). 64bit PHP versions do support higher values.php 仅支持有符号整数。您也看到了溢出
,python 案例的答案应该是
33963677451
php only supports signed integers. You are seeing overflow
also, the answer for the python case should be
33963677451
我不知道这是否只是一个拼写错误,但在每种语言中执行 AND 运算时您使用了不同的值。 :)
PHP 版本中有“0xFFFFFFFFFFF”(11 个字符),而 Python 版本中只有 8 个字符。
无论哪种方式,都应该是整数溢出。
I don't know if it's just a typo, but you are using different values when performing the AND operation in each language. :)
You have "0xFFFFFFFFFFF" (11 chars) in your PHP version, as opposed to only 8 in the Python version.
Either way, it should be integer overflow.
尝试确保 $integer 是 float 类型。
欲了解更多信息: http://www. php.net/manual/de/language.types.integer.php#language.types.integer.overflow
try making sure that $integer is of type float.
for more: http://www.php.net/manual/de/language.types.integer.php#language.types.integer.overflow
如果您想了解发生了什么,这里有一个显示不同类型的 printf 语句。
PHP 将大于 32 位的整数转换为浮点数。看来你不能对浮点数执行此操作。
If you wish to see whats happening, here is a printf statement showing the different types.
PHP casts integers larger than 32 bits to floats. It seems that you can not perform this operation on floats.