Home > Web Front-end > JS Tutorial > Understanding and analysis of JS bitwise NOT (~) operator and ~~ operator_javascript skills

Understanding and analysis of JS bitwise NOT (~) operator and ~~ operator_javascript skills

WBOY
Release: 2016-05-16 18:04:08
Original
1277 people have browsed it

Then, for the type of typeof var!==”number”, when performing operations, it will try to convert into 32-bit integer data. If it cannot be converted into integer data, it will be converted into NaN;
JS uses bitwise operations A simpler way to implement this operation, then its implementation principle can be roughly understood as follows:

Copy code The code is as follows:

var testData=-2.9;
var testResult=(typeof testData==="number"&&!isNaN(testData)&&testData!==Infinity)?(testData>0 )?-Math.floor(testData)-1:-Math.ceil(testData)-1:-1;

First of all, if a data is trying to be converted to 32 integer data, the result is < ;0, then it needs to be rounded up, such as -2.9->-2, if >0, it is rounded down, such as: 2.6->2;
If a data cannot be converted to 32 bit binary representation, it is converted to NaN; then converted to -1; for example ~{}/~NaN ==-1;
and for example ~function(){return 100;}->-1;
In Jquery, it is useful to use if(!~this.className.indexOf(str)){ //do some thing…..}; Here, the return value of this.className.indexOf(str) is either greater than -1 , or it is equal to -1; when it is equal to -1, ~-1===0; then, !~-1===true; then it can be concluded that this does not contain the class name str...;
For the ~~ operator, in the same way, it can also be expressed as:
Copy code The code is as follows:

var testData=2.1;
var testResult=(typeof testData==="number"&&!isNaN(testData)&&testData!==Infinity)?(testData>0)?Math.floor(testData): Math.ceil(testData):0;

is also understood by rounding up and down;
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