Some details about bitwise anti-operators in Javascript

Source: Internet
Author: User

I hope this article will be helpful to you in introducing a Javascript bitwise anti-operator article.

Executes bitwise non (evaluate not) operations on an expression.

Result = ~ Expression


Any variable.


Any expression.



All unary operators (such ~ Operators) are calculated according to the following rules:

• If it is applied to an undefined or null expression, a runtime error is thrown.

• Convert an object to a string.

• If possible, convert the string to a number. Otherwise, a running error occurs.

• The Boolean value is regarded as a number (if it is false, it is 0; if it is true, it is 1 ).

The operator is applied to the result number.

~ Operator to view the value in the binary representation of the expression and perform a bit non-operation.

If any one of the bits in the expression is 1, the bit in the result is 0. If any one of the bits in the expression is 0, the bit in the result is changed to 1.

The following example illustrates the bitwise (~) Operator usage.

Var temp = ~ 5;

The value is-6, as shown in the following table.


Binary value (complement of 2)
Decimal Value
00000000 00000000 00000000 00000101
~ 5
11111111 11111111 11111111 11111010

The following example illustrates the bitwise (~) The usage of the operator, which contains binary numbers that indicate negative decimal numbers. If you are not familiar with this operation, please first understand
Var temp = ~ 5;
5 binary 101, full 32 bits
Bitwise Inversion
Because the first one starting with 32 bits is 1, this is a negative number. to convert the binary into a negative number, we need to reverse code it first.
Then, + 1
Convert to 6 in decimal format, and add the symbol to negative number-6.
Alert (temp );
// Pop up [-6]

Bit operations

There are 7 javascript bit operations

& | ^ ~ <>>>>


Bitwise AND (&)

If the two numbers have the same bit of 1, 1 is returned. Otherwise, 0 is returned,

For example, the binary value of 1 & 2 = 0010 is 0000, And the binary value of is.





The result is 0.


By bit or (|)

When only one of the two numbers is 1, it is 1 2 | 1 = 3.







Non-(^) by bit)

The corresponding bitwise of the two operands is 1, otherwise it is 9 ^ 6 = 15







Bit non (~)

Bitwise non-operator "~" Bitwise inversion of operands, which is a single-object Operator

Bitwise NOT, as the name implies, returns the inverse of the operand. ~ 7 =-8


------ ==========> At the beginning, there should be a symbol, so it is-8.



Shift left (<)

The Left shift operator <implements the function of Population 0 by moving the base position to the left. It is a binary operator.

15 <2 = 60

0000 1111

0011 1100


Move left (>)

The Left shift operator ">" is used to add 0 to the Base position of the entire Left Movement. It is a binary operator.

15> 2 = 3

0000 1111

0000 0011

Related Article

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: 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.