Bitwise NOT operator ( ~ )

Discussion in 'Javascript' started by Matt Kruse, May 12, 2009.

  1. Matt Kruse

    Matt Kruse Guest

    I recently came across this:

    if (~str.indexOf('match')) {
    // found it!
    }

    as opposed to:

    if (str.indexOf('match')>-1) {
    // found it!
    }

    I wasn't familiar with the bitwise NOT operator, having never used it
    before or even considering it for testing cases that return -1 when
    "not successful".

    This syntax looks simple, though somewhat obfuscated because most
    people would be unfamiliar with it.

    Any thoughts?

    Matt Kruse
     
    Matt Kruse, May 12, 2009
    #1
    1. Advertising

  2. Matt Kruse <> writes:

    > I recently came across this:
    >
    > if (~str.indexOf('match')) {
    > // found it!
    > }
    >
    > as opposed to:
    >
    > if (str.indexOf('match')>-1) {
    > // found it!
    > }


    I would prefer ">= 0".

    > I wasn't familiar with the bitwise NOT operator, having never used it
    > before or even considering it for testing cases that return -1 when
    > "not successful".


    It should be safe in this case, since you probably don't have strings
    that are Math.pow(2,32)-1 long. If you had, it could fail.

    > This syntax looks simple, though somewhat obfuscated because most
    > people would be unfamiliar with it.


    Agree.

    > Any thoughts?


    Definitly obfuscated, and unnecessarily so (IMO). It's not as
    if ">= 0" is a hard operation to compute (or optimize, if the
    engine wants to).

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, May 12, 2009
    #2
    1. Advertising

  3. On May 12, 7:27 am, Matt Kruse <> wrote:

    > I wasn't familiar with the bitwise NOT operator, having never used it
    > before or even considering it for testing cases that return -1 when
    > "not successful".


    > Any thoughts?


    I've seen the bitwise operators in Flanagan's book and the ECMAScript
    spec. I haven't seen them in any JavaScript programs and haven't used
    them myself when writing JavaScript. I think you'd have to be going
    over the top with optimization if you were interested in bitmasks in
    browser scripts, for example. I wonder why they were added to the
    language at all given the language was designed for toggling images on
    mouseover. Maybe they were added because the first implementation was
    in C and C has the operators so they were sort of freebies to bulk up
    the apparent feature set of the language.

    Peter
     
    Peter Michaux, May 16, 2009
    #3
  4. In comp.lang.javascript message <8b55c4bc-7da0-48cf-99f2-7ef15ec7575c@k9
    g2000pra.googlegroups.com>, Fri, 15 May 2009 22:25:25, Peter Michaux
    <> posted:
    >On May 12, 7:27 am, Matt Kruse <> wrote:
    >
    >> I wasn't familiar with the bitwise NOT operator, having never used it
    >> before or even considering it for testing cases that return -1 when
    >> "not successful".

    >
    >> Any thoughts?

    >
    >I've seen the bitwise operators in Flanagan's book and the ECMAScript
    >spec. I haven't seen them in any JavaScript programs and haven't used
    >them myself when writing JavaScript. I think you'd have to be going
    >over the top with optimization if you were interested in bitmasks in
    >browser scripts, for example. I wonder why they were added to the
    >language at all given the language was designed for toggling images on
    >mouseover. Maybe they were added because the first implementation was
    >in C and C has the operators so they were sort of freebies to bulk up
    >the apparent feature set of the language.



    The language was designed to be generally applicable; it is, for
    example, used server-side and is supported by Windows Script Host.

    Consider a control F.X with a boolean value; it can be nicely toggled by
    F.X.value^=1.

    There are other examples on my site, and a page partly about them.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, May 16, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. littlehobo

    Bitwise operator question...

    littlehobo, Sep 30, 2003, in forum: C++
    Replies:
    10
    Views:
    6,057
    Ron Natalie
    Oct 1, 2003
  2. Mantorok Redgormor

    bitwise not operator

    Mantorok Redgormor, Oct 1, 2003, in forum: C Programming
    Replies:
    1
    Views:
    546
    Dan Pop
    Oct 1, 2003
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,601
    Keith Thompson
    Feb 14, 2005
  4. Ioannis Vranos
    Replies:
    8
    Views:
    349
    James Kanze
    Nov 14, 2008
  5. aleksa

    Bitwise Not Operator

    aleksa, Jun 26, 2012, in forum: C Programming
    Replies:
    5
    Views:
    519
    James Kuyper
    Jun 26, 2012
Loading...

Share This Page