! in arithmatic calculations ?

meow00
meow00 used Ask the Experts™
on
To C++ Experts,

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

meow.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what are you trying to do? the & symbol is for bitwise and, not the logic and (&&) I hope you know that.
Commented:
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*
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial