JavaScript32-bit integer unsigned operation

Source: Internet
Author: User
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-bitSignedInteger, 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 LanguageAndJS.

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>>>0The shift operation is fine.

Not recommended>>, Recommended>>>Because the leftmost digit is parsed as a symbol, when the number overflows, It is parsedNegative.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.