开发者

~ in javascript [duplicate]

开发者 https://www.devze.com 2023-03-30 18:27 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to: The ~ operator?
This question already has answers here: Closed 11 years ago.

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号