Need help understanding !NOT

This is a discussion on Need help understanding !NOT within the C++ Programming forums, part of the General Programming Boards category; Good afternoon everyone. I have a simple assignment statement: var=((5*67)+!50) My problem is understanding the ! (NOT). If something is ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    5

    Need help understanding !NOT

    Good afternoon everyone.

    I have a simple assignment statement:

    var=((5*67)+!50)

    My problem is understanding the ! (NOT). If something is not 50, it is false, which returns a value of 0. I would calculate var=335. Is this correct? I know the !50 is a logical operation, which I read as returning TRUE or False. My difficulty is converting that T/F return value (0 or 1) and using it as part of an arithmetic calculation. Is there some place I can read up on this to help grasp the concept?

    Thanks for your help

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Umm... Will this do it for you?
    Code:
    int convert(bool in) {
       return ((in) ? 1 : 0);
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,747
    !0 is 1
    ! of anything which isn't zero is 0

    Imagine !x as if ( x == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by nickadeemus2002
    Good afternoon everyone.
    var=((5*67)+!50)
    Code:
    var = ((5*67)+!50);
    var = ((5*67)+0);
    Are the same. Indeed your example is just a waste of code since you would normally just do

    Code:
    var = 335; // as you had previously mentioned
    You should use the ! operator with variables, not constants.

    Code:
    var = !x; // acceptable code
    var = !0; // code that only will generate confusion later.
    There is nothing wrong with using ! with constants except for the fact that it obfuscates your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 06:53 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 07:46 PM
  4. Replies: 8
    Last Post: 10-24-2005, 12:26 PM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21