javascript - How does !!~ (not not tilde/bang bang tilde) alter the result of a 'contains/included' Array method call? -
if read comments @ jquery inarray
page here, there's interesting declaration:
!!~jquery.inarray(elm, arr)
now, believe double-exclamation point convert result type boolean
, value of true
. don't understand use of tilde (~
) operator in of this?
var arr = ["one", "two", "three"]; if (jquery.inarray("one", arr) > -1) { alert("found"); }
refactoring if
statement:
if (!!~jquery.inarray("one", arr)) { alert("found"); }
breakdown:
jquery.inarray("one", arr) // 0 ~jquery.inarray("one", arr) // -1 (why?) !~jquery.inarray("one", arr) // false !!~jquery.inarray("one", arr) // true
i noticed if put tilde in front, result -2
.
~!!~jquery.inarray("one", arr) // -2
i don't understand purpose of tilde here. can please explain or point me towards resource?
the tilde operator isn't part of jquery @ - it's bitwise not operator in javascript itself.
see the great mystery of tilde(~).
you getting strange numbers in experiments because performing bitwise logical operation on integer (which, know, may stored two's complement or that...)
two's complement explains how represent number in binary. think right.
Comments
Post a Comment