Solved

! in arithmatic calculations ?

Posted on 2003-10-27
3
197 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
[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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

756 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