Home > Article > Web Front-end > How to perform bitwise negation in javascript
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.
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
The following figure analyzes the process of performing bitwise NOT operation on 12 in the form of arithmetic formula.
How to calculate bitwise inversion
Bitwise inversion operation rule steps:
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
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
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
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 ----> 10000010After 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 ----> 000000005. Convert the original code to binary
十进制 ----> 原码 ----> 反码 ----> 补码 ----> 补码取反 ----> 取反补码转成反码 ----> 转成原码 ----> 转成二进制 1 ----> 00000001 ----> 0000001 ----> 00000001 ----> 11111110 ----> 11111101 ----> 10000010 ----> -2
十进制 ----> 原码 ----> 反码 ----> 补码 ----> 补码取反得原码 ----> 转成二进制 -1 ----> 10000001 ----> 11111110 ----> 11111111 ----> 00000000 ----> 0
The role of negating and then negating~~
~~ (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【Related recommendations:
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!