?
Solved

!! operator

Posted on 2003-11-17
5
Medium Priority
?
329 Views
Last Modified: 2012-05-04
edit - - i get it
0
Comment
Question by:jini555
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9766495
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*
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 200 total points
ID: 9766534


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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9766540

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

:)

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9766594
Kent:

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

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

Dex*
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9766631

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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question