Solved

! in arithmatic calculations ?

Posted on 2003-10-27
3
193 Views
Last Modified: 2010-04-02
To C++ Experts,

   How do I calculate things like :
  int x = !(!3 & !8) ;
 thanks ....

meow.
0
Comment
Question by:meow00
3 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9630235
what are you trying to do? the & symbol is for bitwise and, not the logic and (&&) I hope you know that.
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 40 total points
ID: 9630259
meow00:

> How do I calculate things like :
>   int x = !(!3 & !8) ;

Okay, let's break it down.

!3 = 0 because 3 = True, and !True = False, and False = 0
!8 = 0 because 8 = True, and !True = False, and False = 0

(!3 & !8) = (0 & 0) = 0

!(0) = !(False) = True = 1

So, int x would have the value of 1.

Hope that helps,
Dex*
0
 

Expert Comment

by:nesrohtretep
ID: 9630426
You are mixing logical (!) and bitwise (&) operators in an expression. It is a very error-prone practice because of the implicit conversions back-and forth so I'd advise you to stick to one or the other or make explicit conversions.

Are you sure that you don't mean to use the bitwise negation (~) rather than ! (logical negation). ~ will revert each individual bit whereas ! always will result in either 0 or 1.

 - Rgds Peter
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now