~ in javascript [duplicate]
Possible Duplicate:
How to: The ~ operator?
What's the ~ operator does in javascript?
input
alert(~1)
output is
-2
input
~function () {}()
output is
-1
I never heard about ~ operator in javascript开发者_如何学C
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
Bitwise NOT ~ a Inverts the bits of its operand.
I guess its fairly odd that a function returns -1, but what would you expect anyway.
This is the bitwise not operator that inverts the value of every bit in the integer. In binary a signed integer has the following representation:
00000001 = 1
11111110 = -2
See this wikipedia article.
The bitwise NOT operator (~) will take its operand, convert it to a 32-bit integer, and will invert each bit so that each 0 becomes a 1 and vice versa.
http://james.padolsey.com/javascript/double-bitwise-not/
精彩评论