What does ~~ (“double tilde”) do in Javascript?

前端 未结 9 746
有刺的猬
有刺的猬 2020-11-22 17:16

I was checking out an online game physics library today and came across the ~~ operator. I know a single ~ is a bitwise NOT, would that make ~~ a NOT of a NOT, which would

9条回答
  •  执笔经年
    2020-11-22 17:44

    Tilde(~) has an algorihm -(N+1)

    For examle:

    ~0 = -(0+1) = -1
    ~5 = -(5+1) = -6
    ~-7 = -(-7+1) = 6
    

    Double tilde is -(-(N+1)+1)

    For example:

    ~~5 = -(-(5+1)+1) = 5
    ~~-3 = -(-(-3+1)+1) = -3
    

    Triple tilde is -(-(-(N+1)+1)+1)

    For example:

    ~~~2 = -(-(-(2+1)+1)+1) = -3
    ~~~3 = -(-(-(3+1)+1)+1) = -4
    

提交回复
热议问题