Reputation: 2308
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.
Upvotes: 1
Views: 91
Reputation: 10392
! 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).
Upvotes: 0
Reputation: 105885
!
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.
Upvotes: 0
Reputation: 167182
~ (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.
Upvotes: 1