Bitwise NOT operator ( ~ )

M

Matt Kruse

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
 
L

Lasse Reichstein Nielsen

Matt Kruse said:
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
 
P

Peter Michaux

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
 
D

Dr J R Stockton

In comp.lang.javascript message <8b55c4bc-7da0-48cf-99f2-7ef15ec7575c@k9
g2000pra.googlegroups.com>, Fri, 15 May 2009 22:25:25, Peter Michaux
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top