SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    What does this mean in javascript

    I found this variable declaration in an open source project:

    Code:
    var index = ~~(i/BUFFER_LENGTH);
    What does the "~~" mean? I can't find answer on Google.

    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,872
    Mentioned
    206 Post(s)
    Tagged
    12 Thread(s)
    Howdy,

    The double tilde ~~ is a double NOT bitwise operator.
    It is used as a faster substitute for Math.floor() (in that it just removes anything to the right of the decimal).

    See here for a more indepth explanation: http://stackoverflow.com/questions/4...-in-javascript

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,872
    Mentioned
    206 Post(s)
    Tagged
    12 Thread(s)
    No probs

    Just to expand on my above example:

    Positive number:

    Code JavaScript:
    var pi = 3.14159265359;
    console.log(pi);  //3.14159265359
    console.log(Math.floor(pi));  //3
    console.log(~~pi); //3

    Negative number:

    Code JavaScript:
    var negPi = -3.14159265359;
    console.log(negPi );  //-3.14159265359
    console.log(Math.floor(negPi ));  //-4
    console.log(~~negPi ); //-3


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •