Simple question

This is a discussion on Simple question within the C++ Programming forums, part of the General Programming Boards category; Ok, I'm having some serious issues with Tutorial 2 and the whole ! and && with 0's and 1's. I ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Simple question

    Ok, I'm having some serious issues with Tutorial 2 and the whole ! and && with 0's and 1's. I got a 100% on the quiz but I don't even know how i did the evaluate this code
    Code:
     !(1&&(1||0));
    Please help, I'm utterly confused. I understand the &&, !, and ||. I know Java well but this is just wierd to me.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    ! (  1  && (  1  ||  0  ) );
    Starting with the inner most parenthesis. TRUE OR FALSE is TRUE, TRUE AND TRUE is TRUE, NOT TRUE is FALSE.
    Last edited by SlyMaelstrom; 03-04-2006 at 08:54 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Ok I think I get it. One last question, if for instance I was typing a code and wanted an if statement such as..

    Code:
    if !(x==1 || x==0)
    
    {
    //do a bunch of nonsense herer
    }
    Would i recive a 1? I understand how to figure it out now though. Thanks for that.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1 = true
    0 = false

    so the conditional is equivalent to:
    Code:
    !(  true && (true||false)  );

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by mithrillegolas
    Ok I think I get it. One last question, if for instance I was typing a code and wanted an if statement such as..

    Code:
    if (!(x==1 || x==0))
    
    {
    //do a bunch of nonsense herer
    }
    Would i recive a 1?
    Well, with the outer parenthesis to make the syntax correct and assuming at least one of those comparisons is true, then the result would be false. If x didn't equal 1 and x didn't equal 0, then the result would be true.
    Last edited by SlyMaelstrom; 03-04-2006 at 09:00 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Would i recive a 1?
    You wouldn't receive anything.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by 7stud
    You wouldn't receive anything.
    Well... he might receive a mild shock when he realizes it doesn't compile.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Errr, ok. I get it, I was more referring to the fact of 1 and 0 being pre-defined and we can't touch them. But that was a stupid assumption. I understand now.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If you want to see what the result of a conditional is, you can always display it:
    Code:
    int x = 10;
    cout<<(x==1 || x==0)<<endl;

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    it might also help by thinking of how logic gates work. the 8 basic logic operations (2 unary and 6 binary) implemented as inline functions would be:

    Code:
    inline
    bool
    relay(bool a)
    {
    	return a;
    }
    
    inline
    bool
    not(bool a)
    {
    	return !a;
    }
    
    inline
    bool
    and(bool a, bool b)
    {
    	return a & b;
    }
    
    inline
    bool
    or(bool a, bool b)
    {
    	return a | b;
    }
    
    inline
    bool
    xor(bool a, bool b)
    {
    	return a ^ b;
    }
    
    inline
    bool
    nand(bool a, bool b)
    {
    	return not(and(a, b));
    }
    
    inline
    bool
    nor(bool a, bool b)
    {
    	return not(or(a, b));
    }
    
    inline
    bool
    xnor(bool a, bool b)
    {
    	return not(xor(a, b));
    }
    
    /* eg: */
    
    int
    main(void)
    {
    	/* !(1&&(1||0)) */
    	cout << nand(1, or(1, 0)) << endl;
    }
    also, by memorizing their truth tables and learning boolean algebra you will be able to verify and simplify boolean expressions with greater ease.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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