简体   繁体   English

在Java的位掩码标志值中可以安全使用的最大整数是多少?

[英]What is the maximum integer it is safe to use in a Javascript bitmask flag value?

This is mostly just a sanity-check. 这主要只是一个健全性检查。

Mozilla says that Mozilla

The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format. 所有按位运算符的操作数都将转换为二进制补码格式的有符号32位整数。

and that 然后

The numbers -2147483648 and 2147483647 are the minimum and the maximum integers representable through a 32bit signed number. 数字-2147483648和2147483647是可通过32位带符号数字表示的最小和最大整数。

Since 2147483647 is 0x7FFFFFFF, I believe that 0x40000000 (that is to say, not 0x80000000) is the maximum number I can safely use as a javascript flag value. 由于2147483647是0x7FFFFFFF,我相信0x40000000(也就是说, 不是 0x80000000)是我可以安全地用作javascript标志值的最大数量。 But I'd like to make sure I haven't missed something or that there aren't other gotchas. 但是我想确保我没有错过任何东西,或者没有其他陷阱。 Thank you in advance! 先感谢您!

The value range is a complete 32-bit value, ie. 值范围是一个完整的32位值,即。 0 to 0xffffffff (or 2 32 -1). 0至0xffffffff(或2 32 -1)。 If it will be signed or not depends. 是否将签署取决于。 If it will be signed initially then this will produce -1: 如果最初将其签名,则将产生-1:

 document.write(0xffffffff>>0); 

But you can use unsigned values too which means the range is [0, 4294967295]: 但是您也可以使用无符号值,这意味着范围是[0,4294967295]:

 document.write(0xffffffff>>>0); 

The number 0x40000000 is only gonna give you half your range (in the negative range, in the positive it would be 0x40000000-1, or 0x3fffffff) so this is not the safe number for a 32-bit signed range. 数字0x40000000只会给您一半的范围(在负数范围内,在正数范围内将是0x40000000-1或0x3fffffff),因此对于32位有符号范围来说,这不是安全的数字。

You safe-range for signed number would be [0x80000000, 0x7fffffff], so the common safe-margin mask would be 0x7fffffff, however, you would need to preserve the sign-bit: 您签名数字的安全范围为[0x80000000,0x7fffffff],因此常见的安全边距掩码为0x7fffffff,但是,您需要保留符号位:

number = number < 0 ? number & 0xffffffff : 0x7fffffff;

And for unsigned your mask would always be 0xffffffff. 对于未签名,您的掩码将始终为0xffffffff。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM