JavaScript 32-bit integer unsigned operation example

Source: Internet
Author: User
Tags bitwise numeric numeric value

All integer variables default to signed integers, JavaScript bitwise operation, is the use of 32-bit signed integer, which means that the result of its conversion is also a 32-bit signed integral type

In JavaScript, all integer numeric variables are signed by default, what does that mean?  A signed integer uses a 31-bit numeric value that represents an integer, a symbol with a 32nd digit integer, 0 for a positive number, and a 1 for negative numbers.    The range of values ranges from -2^31-2^31-1 2147483648 to 2147483647. When JavaScript is bit-operated, a 32-bit signed integer is used, which means that the result of the conversion is also a 32-bit signed integer.    In some cases, we will have unexpected results of the shift, the following is the C language and JS contrast.  C language code is as follows: unsigned int a = 3774191835u;  unsigned int b = a >> 2;  /* b = = 943547958 * * JavaScript code is as follows: var a = 3774191835;  var B = a >> 2; /* b = = 130193866/Can be seen, JavaScript for bitwise operation, is the use of signed integer type, so we got a different result.    How to solve it? We can convert the number of symbols in JavaScript into unsigned numbers.    Just need to do >>>0 shift operation is good. It is best not to use >>, recommended >>> because the leftmost one will be resolved into a sign bit, when the number overflows, will be resolved into negative numbers.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.