Javascript Bitwise-Bitwise COUNTER Operator (~) _ Basics

Source: Internet
Author: User
Tags bitwise

Copy Code code as follows:

result = ~ "Number"

All unary operators, such as the ~ operator, calculate the value of an expression according to the following rules:

Copy Code code as follows:

1. If applied to an undefined expression or null expression, a run-time error is raised.
2, convert the object to a string.
3, if possible, converts a string to a number. Otherwise, a run-time error is raised.
4, the Boolean value is treated as a number (if False, 0, or 1 if true).

Operator will be applied to the result number.

The ~ operator looks at the value of the binary representation of the expression and performs a bitwise NON operation.

If any one in the expression is 1, the bit in the result becomes 0. If any one in the expression is 0, the bit in the result becomes 1.

The following example illustrates the use of the bitwise NON (~) operator, which contains a binary that represents a decimal number, and if you are unfamiliar with this, read the decimal negative conversion to binary, octal, hex.

Copy Code code as follows:

var temp = ~5;
/*
52 in 101, filling up 32 bits
00000000000000000000000000000101
Reverse by position
11111111111111111111111111111010
Since the 32-bit first is 1, this is a negative number, which converts the binary to a negative number, which requires an inverse code
00000000000000000000000000000101
After that, another +1
00000000000000000000000000000110
Convert to decimal 6, plus symbol to negative-6
*/
Alert (temp);
Pop-up "-6"

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