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

Javascript bitwise negation operator (~)_Basic knowledge

WBOY
Release: 2016-05-16 17:01:36
Original
1897 people have browsed it

Copy code The code is as follows:

result = ~ [Number]

All unary operators (such as the ~ operator) evaluate expressions according to the following rules:

Copy code The code is as follows:

1. If applied to an undefined expression or null expression, a runtime error is raised.
2. Convert the object to a string.
3. If possible, convert the string to a number. Otherwise, a runtime error will be thrown.
4. Boolean values ​​are treated as numbers (0 if false; 1 if true).
The

operator will be applied to the resulting number.

The

~ operator looks at the value of the binary representation of an expression and performs a bitwise NOT operation.

If any bit in the expression is 1, that bit in the result becomes 0. If any bit in the expression is 0, that bit becomes 1 in the result.

The following example illustrates the usage of the bitwise NOT (~) operator, which includes binary representation of negative decimal numbers. If you are not familiar with this, please read " Conversion of negative decimal numbers into binary, octal, and hexadecimal 》.

Copy code The code is as follows:

var temp = ~5;
/*
5 Binary 101, fill up 32 bits
0000000000000000000000000000101
Negrate bitwise
111111111111111111111111111111010
Since the first 32 bits start with 1 , so this is a negative number, convert binary to a negative number, You need to first invert
0000000000000000000000000000101
, then 1
000000000000000000000000000000110
to convert to decimal 6, add the sign to become a negative number -6
*/
alert(temp);
// Pop up【-6】
Related labels:
js
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