Bitwise inverse operator

Source: Internet
Author: User

Today you see a piece of code in the project:

if (~key.indexof (' I ')) {    Pricetagdata.adulti = {     tag:key, price:value}}

I say to this operator is very strange, naturally also can not understand the author's intentions. Now that my curiosity is in a vigorous period, I decided to learn the usefulness of this operator.

Looked at the eye ES5, originally it is the bit to take the counter-operator. The following is a description of the ES5 operator:

Bitwise not Operator (~)

The production unaryexpression: ~ unaryexpression is evaluated as follows:

1.Let expr be the result of evaluating unaryexpression.
2.Let oldValue be ToInt32 (GetValue (expr)).
3.Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.

The approximate evaluation process is:

1. First Evaluate ~ Right expression, set evaluation result to expr

2. Execute ToInt32 (expr), set the return result to OldValue

Because the number type storage format in ECMAScript is a 64-bit floating-point number, the floating-point number is converted to a 32-bit signed integer before the bitwise operation, which is the ToInt32 method in the above description. ToInt32 is the canonical definition of the internal type conversion method, ECMAScript program users can not directly invoke, ToInt32 specific points here .

3. Reverse and return the OldValue bitwise

Because a 32-bit integer is returned, you also need to convert the integer to a 64-bit floating-point number. Thus, the bit operation in ECMAScript does not operate as directly as c,c++, it requires the operation of the floating-point numbers to be converted to integers and integers before and after the bitwise operation.

Look at a specific example: ~4294962318, we use the definition of the above specification to make the result ...

The first evaluates to the right expression, after the evaluation is 4294962318,

Then execute ToInt32 (4294962318),

The internal execution process for ToInt32 is:

Convert 4294962318 to Tonumber and the result is still 4294962318

Evaluation sign (4294962318) * Floor (ABS (4294962318)), the result is still 4294962318

Evaluates to a value of 4294962318 modulo 4294967296 (2^32) with a result of 4294962318

4294962318 is greater than 2147483648 (2^31), so return 4294962318-4294967296, i.e.-4978

Sign, floor, ABS, modulo usage see

Next to convert 4978 into 2, in order to facilitate my writing a paragraph program to convert a 10 into 2, below is the C + + source, you can use this program to convert the number you want to convert

#include <iostream>using namespace std;    Char str[255]; itoa (T, str, 2); cout << str << endl; system (return 0;} 

4978 in binary notation 1111 1111 1111 1111 1110 1100 1000 1110

The bitwise inversion is 0000 0000 0000 0000 0001 0011 0111 0001

The result of this inversion is theoretically the value computed by the ~4294962318.

Next we execute ~4294962318 in the JS console and get 4977

Re-use the program to convert 4977 into binary results is 0000 0000 0000 0000 0001 0011 0111 0001

You'll find that the binary that we derive is the same.

After the theory and practice have been told, we go back to the original question:

if (~key.indexof (' I ')) {

What exactly does the author want to do?

IndexOf everyone should know that it is a method of a string that is used to find the position of the first occurrence of the passed character in the string, and if so, returns the index value of the character in the string if there is no return-1.

~-1 is exactly equal to 0, and 0 equals false in the ECMAScript if condition, and not 0 equals true. Finally!!! I understand the author's intention, that is, if the key string contains I, then execute the IF condition is true in the statement, if it does not contain I, then do not execute the IF condition is true in the statement.

The equivalent of this high-handsome and rich notation of the cock silk is:

if (Key.indexof (' I ')!==-1) {    Savetheearth ();}

The last is the time to spit out the groove ...

I personally do not agree with this weird notation (as opposed to me). Because jser in peacetime programming need to use bit operation place is relatively few, so in the multi-person project if write such a code, later people to maintain this code will feel confused, virtually increased the difficulty and cost of project maintenance.

Bitwise inverse 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.