JavaScript 中的无符号整数
我正在开发一个处理 IP 地址信息的页面,但它对整数有符号这一事实感到窒息。我使用按位运算符来加速它,但第 64 位(有符号/无符号标志)搞乱了它。
有什么方法可以强制 JavaScript 中的数字不带符号吗?它似乎工作正常,直到子网大于 30 或小于 2。
试试这个:
<html>
<body>
<script type='text/javascript'>
document.write( (1 << 30) +"<br/>");
document.write( (1 << 31) +"<br/>");
document.write( (1 << 32) +"<br/>");
</script>
</body>
</html>
结果:
1073741824 -2147483648 1
I'm working on a page that processes IP address information, but it's choking on the fact that integers are signed. I am using bitwise operators to speed it up, but the 64th bit (signed/unsigned flag) is messing it up.
Is there any way to force a number to be unsigned in Javascript? It seems to work fine, until subnet is greater than 30, or less than 2.
Try this:
<html>
<body>
<script type='text/javascript'>
document.write( (1 << 30) +"<br/>");
document.write( (1 << 31) +"<br/>");
document.write( (1 << 32) +"<br/>");
</script>
</body>
</html>
Result:
1073741824
-2147483648
1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
<<
运算符定义为处理带符号的 32 位整数(从双精度浮点数的本机数字存储转换而来)。因此1<<31
的结果一定是负数。唯一使用无符号 32 位整数的 JavaScript 运算符是
>>
。您可以利用此功能将您一直在使用其他按位运算符处理的有符号整数转换为无符号整数:同时:
不起作用,因为所有移位操作仅使用最低的 5移位位(在 JavaScript 和其他类似 C 的语言中也是如此)。
<<32
等于<<0
,即。没有变化。The
<<
operator is defined as working on signed 32-bit integers (converted from the native Number storage of double-precision float). So1<<31
must result in a negative number.The only JavaScript operator that works using unsigned 32-bit integers is
>>>
. You can exploit this to convert a signed-integer-in-Number you've been working on with the other bitwise operators to an unsigned-integer-in-Number:Meanwhile:
won't work because all shift operations use only the lowest 5 bits of shift (in JavaScript and other C-like languages too).
<<32
is equal to<<0
, ie. no change.Douglas Crockford 认为按位运算符是 javascript 的糟糕部分之一:
您确定按位运算符确实可以加速您的逻辑吗?
Douglas Crockford believes that bitwise operators is one of the bad parts of javascript:
Are you sure that bitwise operators really speed up your logic?
使用>>而不是>>获得无符号右移而不是符号扩展右移。无论整数是否带符号,所有其他按位运算符的行为方式都相同。
你的代码破坏“当子网......小于2”是令人担忧的。听起来您可能有一些与整数符号无关的错误。
Use >>> instead of >> to get an unsigned right shift instead of a sign-extending one. All the other bitwise operators behave the same way regardless of whether ints are signed or not.
Your code breaking "when subnet ... is less than 2" is concerning. That sounds like you may have some bug unrelated to signedness of integers.
JavaScript 没有整数,所有数字实际上都是双精度数。
Mozilla 的 Javascript 1.5 Reference 建议只能使用按位运算安全地处理 32 位数字。
Javascript doesn't have integers, all numbers are actually doubles.
The Javascript 1.5 Reference by Mozilla suggests that one can only use bitwise-operations safely for 32 bit numbers.
以下是在 javascript 中将 ipv4 地址与无符号整数相互转换的两个函数:
Here are two functions that convert ipv4 addresses to/from unsigned integers in javascript:
您有哪些类型的 IP 地址? IPv4 仅使用 32 位地址,因此 JavaScript 应该没问题(使用 double 可以为您提供 52 位整数部分< /a>)。 IPv6 使用 128 位地址,因此您必须使用数组。我的猜测是其他东西坏了。
[编辑] 构建一个小型库,它使用两个整数的数组作为内部数据类型。
What kind of IP addresses do you have? IPv4 uses only 32bit addresses, so JavaScript should be fine (using double which gives you an 52bit integer part). IPv6 uses 128bit addresses, so you'll have to use an array. My guess is that something else is broken.
[EDIT] Build a small library which uses an array of two ints as the internal data type.
Javascript现在有一个
bigint
类型,你可以使用n
后缀来组成这样的数字。所以使用你的例子你可以这样做:
Javascript now has a
bigint
type, you can use then
suffix to make such a number.So using your example you could do: