• C

# !! operator

edit - - i get it
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
jini555:

> int i = !! (SHORT) j;

! = Boolean NOT
!! = Boolean NOT NOT

It's silly, but that's what it means.  You should avoid using it.  It is the same as saying this:
int i = (j != 0);

Hope That Helps,
Dex*
DBACommented:

Depending on the compiler, optimization, and the mood of the guy implementing this, the two "!" operators may cancel each other out.

But in real life, they don't.  The first "!" operator (actually, the righter-most of the two) implicitly converts (short)j to a boolean value so that the result will be 0 or 1.  The second "!" operator then complements the value so that the net effect is the same as:

int i = ((short)j) ? 1 : 0;

Kent

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

DBACommented:

Isn't it amazing how many "other ways" there are to do things in C?

:)

Commented:
Kent:

Yeah it is.  These two are equivilant:
int i = ((short)j) ? 1 : 0;

And:
int i = (j != 0);

Dex*
DBACommented:

Actually, they're not.

if *j* is a variable type that is longer than a short, then your test MAY fail depending on the value of *j*.  You need to recast it as a short (to truncate the upper bits) before testing its value.

But with that small change, we do have three ways to do the same thing that I'd do in two lines of assembler.  :)

Kent
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.