The comprehension analysis of JS bitwise non (~) operator and ~ ~ Operator _javascript Skill

Source: Internet
Author: User
Tags bitwise
Then, for the type of typeof var!== "number", an attempt is made to convert to a 32-bit shape data, which is converted to Nan if it cannot be converted into a shaping data;
JS in-situ operation with a more convenient way to implement this operation, then its implementation principle can be generally understood as follows:
Copy Code code 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 <0 when it tries to convert to 32, then it needs to be rounded up, such as -2.9->-2, if >0, round it, 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, such as ~{}/~nan ==-1;
Another example is ~function () {return 100;} ->-1;
In jquery, it works like if (!~this.classname.indexof (str)) {//do some thing ...}; Here, the return value for This.className.indexOf (str) is either greater than-1 or equal to 1; At its equal to-1, ~-1===0; then,!~-1===true; Then you can conclude that this does not contain the class name of STR ... ;
For the ~ ~ operator, similarly, it can also be expressed as:
Copy Code code as follows:

var testdata=2.1;
var testresult= (typeof testdata=== "number" &&!isnan (testData) &&testdata!==infinity)? (testdata>0)? Math.floor (TestData): Math.ceil (TestData): 0;

The same method of rounding up and down to understand;

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.