The understanding analysis of JS bitwise non (~) operator and ~ ~ operator

Source: Internet
Author: User
Tags bitwise

Bitwise non-operators, the simple understanding is to change the number of operands and subtract 1, of course, it is simply a simple understanding of the data can be converted to type numbers.

Then, for the type of typeof var!== "number", when the operation is performed, it will attempt to convert to 32-bit shaping data, and if it cannot be converted into the shaping data, it will be converted to Nan;

JS is a more convenient way to implement this operation in the bitwise operation, then its implementation principle can be understood in general:

var testdata=-2.9var testresult= (typeof

First, if a data is <0 when it tries to convert to 32 shaping data, it needs to be rounded up, such as -2.9->-2, if >0, rounding it down, 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; for example ~{}/~nan ==-1;
Another example is ~function () {return 100;} ->-1;
In jquery, useful for example if (!~this.classname.indexof (str)) {//do some thing ...}; Here, for the return value of This.className.indexOf (str), it is either greater than-1, or equal to 1, at the time it equals-1, ~-1===0; then,!~-1===true; Then you can conclude that this does not include the class name Str ... ;
For the ~ ~ operator, in the same vein, it can also be expressed as:

var testdata=2.1var testresult= (typeof

The same way the upper and lower rounding is used to understand;

The understanding analysis of JS bitwise non (~) operator and ~ ~ operator

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.