Home > Web Front-end > JS Tutorial > body text

How to perform bitwise negation in javascript

青灯夜游
Release: 2021-12-07 12:12:28
Original
3248 people have browsed it

In JavaScript, you can use the "~" operator to implement bitwise negation, the syntax is "~ operand"; the "~" operator can convert the specified operand into a binary integer form, and then The binary operand is inverted bit by bit.

How to perform bitwise negation in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the "~" operator to implement bitwise negation. For example, ~1 = -2, ~-3=2, ~true=-2, ~false=-1

The "~" operator can view the binary representation of the specified value and compare the binary operand Perform negation operation bit by bit

  • Step 1: Convert the operand into a 32-bit binary integer.

  • Step 2: Perform inversion operation bit by bit.

  • Step 3: Convert binary one's complement to decimal floating point number.

Perform bitwise NOT operation on 12, the return value is -13.

console.log( ~ 12 );  //返回值-13
Copy after login

The following figure analyzes the process of performing bitwise NOT operation on 12 in the form of arithmetic formula.

How to perform bitwise negation in javascript

How to calculate bitwise inversion

Bitwise inversion operation rule steps:

 1. Convert decimal to original code

 Convert to original binary code, the highest bit is the sign bit, 0 is a positive number, 1 is a negative number

    十进制   ---->  原码
      1   ---->  00000001
     -1   ---->  10000001
Copy after login

 2. Convert the original code to The complement code

The complement code of a positive number is the original code, the complement code of a negative number keeps the sign bit unchanged, and the remaining bits are inverted

    十进制  ---->   原码    ---->  反码
      1    ----> 00000001 ----> 00000001
     -1    ----> 10000001 ----> 11111110
Copy after login

 3. Convert the complement code to the complement code

The complement of a positive number is still the original code. The complement of a negative number is the complement of the complement plus 1

    十进制  ---->    原码   ---->   反码   ---->  补码
      1    ----> 00000001 ----> 00000001 ----> 00000001
     -1    ----> 10000001 ----> 11111110 ----> 11111111
Copy after login

4. The complement is inverted to get the original code

The sign position of the integerafter inverting its complement is 1, which is a negative integer, so the complement of the negative integer is calculated The original code is obtained by inverse operation

The original code is obtained by the inverse operation code, first convert the inverted complement to the inverted code,

formula: inverted code = complement - 1, then convert the inverted code into the original code, the sign bit remains unchanged, and the other bits are inverted

  十进制  ---->    原码    ---->   反码    ---->   补码     ---->  补码取反   ---->  取反补码转成反码  ---->  转成原码
    1   ----> 00000001  ----> 0000001  ---->  00000001  ----> 11111110   ---->   11111101    ---->  10000010
Copy after login

After inverting the complement of a negative integer, the sign position is 0, which is a positive integer. Since the complement and complement of a positive integer are themselves, there is no need to perform the inverse operation

  十进制  ---->    原码    ---->   反码     ---->   补码      ---->   补码取反得原码  
    -1  ---->  10000001  ----> 11111110  ---->  11111111   ---->     00000000    
Copy after login

5. Convert the original code to binary

 十进制  ---->    原码    ---->   反码    ---->   补码     ---->  补码取反   ---->  取反补码转成反码  ---->  转成原码  ---->  转成二进制
    1   ----> 00000001  ----> 0000001  ---->  00000001  ----> 11111110   ---->   11111101    ---->  10000010 ---->   -2
Copy after login
  十进制  ---->    原码    ---->   反码     ---->   补码      ---->   补码取反得原码   ---->  转成二进制
    -1  ---->  10000001  ----> 11111110  ---->  11111111   ---->    00000000     ---->     0
Copy after login

Therefore, ~1=-2, ~-1=0

The role of negating and then negating~~

Operator ~ means bitwise negation. On the surface,

~~ (reverse and then negate) is meaningless. In fact, it can be done in JS. Convert floating point numbers into integers.

 console.log(~~1.11);          //1
 console.log(~~-25.11);       //-25
Copy after login
【Related recommendations:

javascript learning tutorial

The above is the detailed content of How to perform bitwise negation in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template