Thread: boolean

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    That's such a hard subject. Even I tried to learn the boolean in Wiki. I still have a hard time understanding the logic and the intro, not to mention about the structure. What I know is that it only has true and false function but how it is applied to real world or real situation and it is used in a single piece of program is what I really wanna see in order to understand what it really is and how it really works. Since most of the example could be found regarding boolean structure but they do not tell you why in this area you have to use boolean and how it works. The tutorial here does not really state clearly how it works and it doesn't have any example, it just tells you what it is. I really wanna learn it. Without a good understanding of it, video game design would be doomed.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Computers in general see things are either true or false. Everything is.

    The common logical operators are:
    OR - if one OR the other is true.
    AND - if BOTH are true.
    NOT - it's true when it's NOT true.

    In computers, we use this to solve problems where something has to be combined with something else:

    Code:
    if (input > 0 && input < 5) 
       ... do something  
    else
       ... out of range
    The if-statement is true when input is greater than 0 AND inptu is less than 5 (so the valid range is 1..4 if input is an integer).
    or
    Code:
    if (a == 7 || (b == 8 && c == 9))
    {
       ... do some stuff
    }
    True if either a is 7, or if both (b is 8 and c is 9). It will also (see below) be true if a is 7 and b is 9 and c is 9 - since EITHER side can be true to give a true result.

    Note that OR in computers is "inclusive", not like humans where we see OR mostly as an exclusve combination: Are you married or single, do you want coffee or tea. Those can not be true at the same time (unless you are lying or like strange hot drinks).

    There is a logical operator called "XOR" which is "exclusive or" - it is only available as a binary operator in C and C++ (using the ^ symbol). It is truly a OR in human ways: If one side is true and the other is false, the result is true. But we don't use that in C or C++ boolean logic.

    Computer AND is much more like our "human AND" - both sides need to be true for the result to be true.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 12:24 PM
  3. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 10:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM