问题描述
我的工作是处理IP地址信息的页面上,但它呛上整数签署的事实。我使用的位运算符加快步伐,但第64位(符号/无符号标志)是搞乱起来。
有没有办法来强制Javascript被无符号的一个数字?它似乎做工精细,直到子网超过30大于2,或更小。
试试这个:
< HTML和GT;
<身体GT;<脚本类型=文/ JavaScript的'>
的document.write((1 <的document.write((1 <的document.write((1 <&LT; / SCRIPT&GT;&LT; /身体GT;
&LT; / HTML&GT;
结果:
document.write( (1 << 31) +"<br/>");
The <<
operator is defined as working on signed 32-bit integers (converted from the native Number storage of double-precision float). So 1<<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:
document.write(( (1<<31)>>>0 )+'<br />');
Meanwhile:
document.write( (1 << 32) +"<br/>");
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.
这篇关于在Javascript中无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!