The understanding analysis of JS bitwise non (~) operator and ~ ~ operator

Source: Internet
Author: User
Tags bitwise

~ The operand is reversed, the meaning of two is two times to take the inverse operation, in fact, the original number is equal (the operand is 32 integer range)

Here's an example:

var n = -4.9//4.9n = ~n;console.log (n); // 3n = ~n;console.log (n); // 4

Example 2:

var n = 4.2//4.2n = ~n;console.log (n); // -5n = ~n;console.log (n); // 4

Example 3:

var n = 4//4n = ~n;console.log (n); // -5n = ~n;console.log (n); // 4

First, if a data is <0 when it tries to convert to 32 shaping data, it needs to be rounded up, such as -2.9->-2, if >0, rounding it down, such as:2.6->2;
If a data cannot be converted to a 32-bit binary representation, it is converted to NaN, and then to 1; for example ~{}/~nan ==-1;

Signed integers use 31-bit numeric values that represent integers, a 32nd digit symbol for integers, 0 for positive numbers, and 1 for negative numbers.
The values range from -2^31-2^31-1-2147483648 to 2147483647.
When JavaScript makes bit operations, it uses a 32-bit signed integer, which means that the result of its conversion is also a 32-bit signed integer.

A detailed explanation of the ~ operator:

Converts expression to a 32-bit signed integer, and then the bitwise 1 complement. That is, each bit of 0 in the result is set to 1, and each bit of 1 in the result is set to 0. The result is a signed 32-bit integer. This operator is also referred to as a complement operator or a bitwise complement operator.

For example, a hexadecimal value of 0x7777 is represented as a binary number:

0111011101110111

The binary number of the bitwise inverse of the hexadecimal value ~0x7777 is:

1000100010001000

In hexadecimal, it is 0x8888. Therefore, ~0x7777 is 0x8888.

Reference Address: https://zhidao.baidu.com/question/554279014.html?qq-pf-to=pcqq.c2c

Http://www.jb51.net/article/27852.htm

JS bitwise NON (~) operator and ~ ~ Operator's Comprehension analysis

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.