Thread: Boolean Operators?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    18

    Boolean Operators?

    Hi,
    I'm used to boolean operators and understand that they can be very useful, including in areas such as If...Then statements, etc. but I am thoroughly confused after reading the tutorial on the cprogramming site concerning If...Then statements which included some information on Boolean operators. I wrote a small program to try and test out what the author was saying and the results left me bewildered. Here is the code:
    Code:
    #include <iostream.h>
    int main()
    {
    cout<<(!1); // Not 1.
    cout<<(!0); // Not 0.
    cout<<(1 && 0);
    cout<<(1 || 0);
    return 0;
    }
    The first two make perfect sense - if you have one and your looking for the negation its gonna be 0 and the second one is going to be one. But why does one and zerio (1 && 0) equal 0 and one or zero equal (1 || 0) one? I'm guessing perhaps it is because a number cannot be 1 and 0 but it can be 1 or 0 thus 1 and 0 must be false (0) and 1 or 0 can be true?
    Respectfully,
    David.
    - http://www.civilwarsearch.com/ - Civil War Search Directory.
    - http://www.dhq.nu/hutsell/ - Four Free Computer Wargames.
    - http://www.debaunart.com/ - Original and Print Watercolor Artwork.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm guessing perhaps it is because a number cannot be 1 and 0 but it can be 1 or 0 thus 1 and 0 must be false (0) and 1 or 0 can be true?

    First off, in C++, false=0 and true=1.

    Then, it's just a matter of understanding your truth tables. For the compound statement (A && B) to be true, both A and B have to be true. So, if you have (1 && 0), since both terms aren't true, the compound statement will always be false.

    For the compound statement (A || B) to be true only one term has to be true. So, in the statement (1 || 0), one term is true, so the compound statement will always be true.

    For the first two statements:

    !1 means !(true) which is false or zero, and !0 means !(false) which is true or one.
    Last edited by 7stud; 12-14-2003 at 01:44 AM.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I suggest not getting all your knowledge from the tutorials on this board. Many, if not all, are very much outdated. If you are really interested get a book. Before you ask for book recommendations make a search on the board...there have been many threads about it.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    Thanks for the advice and explanation of Boolean. I'll keep my eyes open for a good C++ book.
    Respectfully,
    David.
    - http://www.civilwarsearch.com/ - Civil War Search Directory.
    - http://www.dhq.nu/hutsell/ - Four Free Computer Wargames.
    - http://www.debaunart.com/ - Original and Print Watercolor Artwork.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Strictly speaking, the statements false=0 and true=1 are false. Integers and booleans are of different types. In a boolean context though, 0 evaluates to false, and 1 (or any non-zero number) evaluates to true.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    An AND gate is like a series circuit, if and only if both switches are closed does current flow, whereas the OR gate is a parallel circuit, if either switch is closed, current flows. With these two gates and the inverter (it's output is reverse of input) you can build NAND, NOR, XOR, and CNC gates. You can use simple combinations of these gates to add binary digits, too...
    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;
    }

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Question

    I have to ask, Sebastiani, what's a CNC gate?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The coincidence gate, it's an XOR gate with an inverted output. It's characteristic is that it outputs true if either both inputs are zero or both are one.
    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;
    }

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    isn't it called an XNOR?

    how would you write out an XOR using only AND,OR, and NOT gates?

    http://educ.queensu.ca/~compsci/unit...c/summary.html <= an neat chart summarizing this stuff and showing symbols, etc.
    Last edited by major_small; 12-15-2003 at 12:50 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That is the correct term, XNOR, but it is sometimes called the coincidence gate because it returns true if both inputs are the same.

    XOR gate:

    You can make an XOR gate by AND'ing the output of an OR gate with a NAND gate.
    Last edited by Sebastiani; 12-15-2003 at 01:01 AM.
    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;
    }

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^oh... interesting... I'm still probably going to try to derive the XOR thing on my own anyway because I'm a loser and I do that stuff (write linked lists, recursive functions, memorize pi, random logic, random trig, etc) in my free time...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can piece together a gate using combinations of them, too.
    A NAND gate can be made using an AND gate with an inverted output, or you can invert the inputs of an OR gate to get one. An XNOR gate is the same.
    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. boolean operators
    By forkpie hat in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 03:35 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 03:45 PM
  4. Boolean operators
    By Trogdor27 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2005, 06:46 AM
  5. boolean operators
    By bj31t in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2004, 08:34 PM