3

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

Community
  • 1
  • 1
wukong
  • 2,250
  • 2
  • 21
  • 30

3 Answers3

5

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.

TJHeuvel
  • 11,944
  • 3
  • 36
  • 46
2

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.

trojanfoe
  • 118,129
  • 19
  • 204
  • 237
1

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/

JohnJohnGa
  • 15,076
  • 17
  • 60
  • 86