Javascript Bitwise counter Operator (~)

Source: Internet
Author: User
Tags bitwise

The Javascript bitwise negation operator (~) performs a bitwise non-negation operation on an expression. such as ~ =-2; ~2 =-3; ~99 =-100

The code is as follows: result = ~ "number" all unary operators, such as the ~ operator, evaluate the value of an expression according to the following rules: The code is 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. The code is as follows: var temp = ~5; /* 52 in 101, fill 32-bit 00000000000000000000000000000101-bit counter 11111111111111111111111111111010 because 32 bit the first is 1, so this is a negative number, Convert binary to negative, you need to reverse code 00000000000000000000000000000101, and then +1 00000000000000000000000000000110 to decimal 6, plus the symbol into a negative number-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.