Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# ! in arithmatic calculations ?

Posted on 2003-10-27
Medium Priority
202 Views
To C++ Experts,

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

meow.
0
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

LVL 4

Expert Comment

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

Dexstar earned 160 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

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

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month7 days, 12 hours left to enroll