在javascript中的Bitshift [英] Bitshift in javascript

查看:101
本文介绍了在javascript中的Bitshift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数字:5799218898。并希望将其右移到13位。

所以,windows-calculator或python给了我:

I've got a really big number: 5799218898. And want to shift it right to 13 bits.
So, windows-calculator or python gives me:


5799218898 >> 13 | 100010100100001110011111100001 >> 13
70791            | 10001010010000111

正如所料。

但是Javascript:

But Javascript:


5799218898 >> 13 | 100010100100001110011111100001 >> 13
183624           | 101100110101001000

我认为是因为javascript中的内部整数表示,但找不到任何相关内容。

I think it because of internal integer representation in javascript, but cannot find anything about that.

推荐答案

在ECMAScript(Javascript)中,按位操作始终为32位。因此5799218898被斩波成32位,变为1504251602.这个整数>> 13给出183624。

In ECMAScript (Javascript) bitwise operations are always in 32-bit. Therefore 5799218898 is chopped into 32-bit which becomes 1504251602. This integer >> 13 gives 183624.

在Python中,它们是任意长度的整数。所以没有问题。

In Python they are arbitrary-length integers. So there's no problem.

(Windows计算器中的数字是64位,足以容纳5799218898。)

(And the numbers in Windows calculator are 64-bit, enough to fit 5799218898.)

(正确的答案应该是707912.)

(And the correct answer should be 707912.)

这篇关于在javascript中的Bitshift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆