1

I am out of theories, i don't know what else to say.

So far i always thought that both changed the value to false.

But, !, changes to true/false.

While, ~ , changes to negative number with 1 less so 2 become -3. Why and how.

Muhammad Umer
  • 2,228
  • 2
  • 18
  • 17
  • take a look at [this answer](http://stackoverflow.com/a/12337406/390819). – Cristian Lupascu Jul 04 '13 at 18:08
  • question: how do you know that 00000000000000000000000000001001 is 9.Why there are 32 bits, is it only javascript related? – Muhammad Umer Jul 04 '13 at 18:13
  • @MuhammadUmer: You clearly haven't clicked the [link](http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.8) in my answer (or the one provided by Praveen): *"The production UnaryExpression : `~` UnaryExpression is evaluated as follows: 1. Let `expr` be the result of evaluating UnaryExpression. 2. Let oldValue be **`ToInt32(GetValue(expr)).`** 3. Return the result of applying bitwise complement to `oldValue`. The result is a **signed 32-bit integer**."* – Zeta Jul 04 '13 at 18:15
  • oh thanks i didn't see your answer than...:D – Muhammad Umer Jul 04 '13 at 18:16

3 Answers3

1

~ (Bitwise NOT)

Performs the NOT operator on each bit. NOT a yields the inverted value (a.k.a. one’s complement) of a. The truth table for the NOT operation is:

a   NOT a
0   1
1   0

Example

9 = 00000000000000000000000000001001 (base 2)
               --------------------------------
~9 = 11111111111111111111111111110110 (base 2) = -10 (base 10)

Answer sourced from The tilde ~ operator in JavaScript.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

! is a logical operator, its result is either true or false, while ~ is a bitwise operator.

If you don't understand why ~number is -number - 1 have a look at two's complement.

I am out of theories, i don't know what else to say.

In this case have a look at the language's documentation.

Zeta
  • 103,620
  • 13
  • 194
  • 236
0

! is a boolean operator, it negates the result of a boolean expression.

~ on the other side is the bitwise not operator, which basically means, it flips all the bits in your operand, say, -1 which is represented by all 1 bits to 0 (which means 0 bits are set).

Francisco Soto
  • 10,277
  • 2
  • 37
  • 46