Techniques for JavaScript bit operators and bitwise operators (1/5)

Source: Internet
Author: User
Tags bitwise bitwise operators numeric value

There are two types of ECMAScript integers, that is, signed integers (allowed with positive and negative numbers) and unsigned integers (only positive numbers are allowed). In ECMAScript, all integer literals by default are signed integers, 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. Values range from 2147483648 to 2147483647.

You can store a signed integer in binary form in two different ways, one for storing positive numbers, and one for storing negative numbers. Positive numbers are stored in true binary form, each of the first 31 digits represents the power of 2, starting at the 1th bit (bit 0), representing 20, and 2nd (bit 1) representing 21. The unused bits are padded with 0, which is negligible. For example, the following illustration shows a representation of the number 18.

The binary version of 18 uses only the first 5 digits, which are the significant digits of the number. To convert a number to a binary string, you can see a valid bit:

var inum = 18;
Alert (inum.tostring (2)); Output "10010" This code only outputs "10010″", not 18 of 32-bit representations. Other digits are not important, because the decimal value can be determined using only the first 5 digits. As shown in the following illustration:

Negative numbers are also stored as binary code, but in the form of binary complement. The steps to compute a digital binary complement are three-step:

1. Determine the binary representation of the nonnegative version of the number (for example, to compute the binary complement of-18, first determine the binary representation of 18)
2. Get binary counter code, that is to replace 0 to 1, replace 1 with 0
3. Add 1 to binary counter code
To determine the binary representation of-18, you must first get a binary representation of 18, as follows:

0000 0000 0000 0000 0000 0000 0001 0010 Next, compute the binary inverse code as follows:

1111 1111 1111 1111 1111 1111 1110 1101 Finally, add 1 to the binary inverse code as follows:

1111 1111 1111 1111 1111 1111 1110 1101
1
---------------------------------------
1111 1111 1111 1111 1111 1111 1110 1110 Therefore, the binary representation of 18 is 1111 1111 1111 1111 1111 1111 1110 1110. Remember that when working with signed integers, the developer cannot access 31 bits.

Interestingly, when a negative integer is converted into a binary string, the ECMAScript is not displayed as a binary complement, but is output in front of the standard binary code of the absolute value of the number plus a minus sign. For example:

var inum =-18;
Alert (inum.tostring (2)); Output "10010" This code outputs " -10010″, not binary complement, which is to avoid access bit 31." For simplicity, ECMAScript handles integers in an easy way, making it unnecessary for developers to care about their usage.

Home 1 2 3 4 5 last

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.