JavaScript整数移位安全性:(n <&lt; 1)!=(n * 2) [英] JavaScript integer shift safety: (n &lt;&lt; 1) != (n * 2)

查看:139
本文介绍了JavaScript整数移位安全性:(n <&lt; 1)!=(n * 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挖掘JS刚刚发现了一些新东西:

Digging JS just discovered something new to me:

n = 0xffffffff
4294967295
n
4294967295
n << 1
-2
n * 2
8589934590
(n << 1) == (n * 2)
false
n + 1
4294967296

这是内置FireFox(51.0.1 64位)调试器的控制台输出.. 。

This is console output of builtin FireFox (51.0.1 64-bit) debugger...

到目前为止我读过的内容(w3school等),不允许我怀疑这种行为。

What I have read so far (w3school, etc), does not allow me to suspect such a behaviour.

没问题还是错过了什么?

..要继续......

推荐答案

n<< b 处理 n ,结果为int 32 ,而 n * 2 处理 n 和<$ h $ =http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions 2 -number-valuerel =nofollow noreferrer> number 。

n << b handles n and the result as int 32, whereas n * 2 handles n and the 2 as number.

请注意 4294967295<< 0 -1

这篇关于JavaScript整数移位安全性:(n <&lt; 1)!=(n * 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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