In JavaScript, all integer variables are signed integers by default. What does this mean? A signed integer uses 31 bits to represent the number of integers & amp; 20540; and 32nd bits to represent the integer. 0 represents the positive number, and 1 represents the negative number. Number & amp; 20540; range:-2 ^
In JavaScript, all integer variables are signed integers by default. What does this mean?
A signed integer uses 31 bits to represent the value of an integer, 32nd bits to represent an integer, 0 represents a positive number, and 1 represents a negative number.
The value range is from-2^31 - 2^31-1
-2147483648 to 2147483647.
JavaScript uses 32-bitSigned
Integer, which means that the conversion result is also a 32-bit signed integer. In some cases, unexpected results may occur when we shift data.C Language
AndJS
.
C Language
1 unsigned int a = 3774191835u;2 unsigned int b = a >> 2;3 /* b == 943547958 */
JavaScript
1 var a = 3774191835;2 var b = a >> 2;3 /* b == -130193866 */
We can see that the bitwise operation in JavaScript uses signed integers, so we get different results. How can this problem be solved?
We can convert the signed number in JavaScript to the unsigned number. Only>>>0
The shift operation is fine.
Not recommended>>
, Recommended>>>
Because the leftmost digit is parsed as a symbol, when the number overflows, It is parsedNegative
.