Why only boolean? Why not quadlean?

This is a discussion on Why only boolean? Why not quadlean? within the Tech Board forums, part of the Community Boards category; No, I'm not crazy!! This thread ( http://cboard.cprogramming.com/cplus...ared-java.html ) got me thinking about boolean conditions. I know what boolean logic ...

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309

    Why only boolean? Why not quadlean?

    No, I'm not crazy!!

    This thread (if statements in c++ compared to java) got me thinking about boolean conditions. I know what boolean logic is, and I'm very comfortable with boolean logic. But I wonder if the higher level languages are selling themselves short?

    He's my thought process.

    I work on mainframes daily. I code a LOT in assembler. In assembler, as some of you must know, you work with condition codes set by individual instructions. On the mainframe, there are two bits used to hold the condition code of condition-code-setting instructions. With these 2 bits you get 4 combinations ("quad") of conditions to test for. Now, not all instructions set all possible conditions, but a lot do, and in assembler, you get to take the branch you need after you've tested for the condition code you desire.

    Why is it that this concept of 4 conditions codes (aka 4 possible code paths) has been dumbed down into a true or false in higher level languages?

    In assembler to compare two integers, you just compare them. Then, you branch if equal, less than, greater than, or not equal. In C, for example, you compare against EQ, LT, GT, or NE. If I want to test EQ, LT and GT, then that's two compares I have to code. In asm, it would be one compare and then two possible branches. Simpler.

    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Dino View Post
    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Yes. This sort of optimization is typically done midway through code optimization. The condition flags can be represented as boolean temporaries, and the compiler is aware that more than one temporary can be set by a single comparison. The redundant comparisons can then be eliminated during data flow analysis.

    It's actually a fairly easy optimization to make.

    And of course, the proof:

    Code:
    void a();
    void b();
    void c();
    
    void foo( int x )
    {
        if( x < 0 ) a();
        else if( x == 0 ) b();
        else c();
    }
    Compiled with gcc -O3 -S:

    Code:
    	.file	"foo.c"
    	.text
    	.p2align 4,,15
    .globl foo
    	.type	foo, @function
    foo:
    	pushl	%ebp
    	movl	%esp, %ebp
    	cmpl	$0, 8(%ebp)
    	jl	.L9
    	je	.L10
    	popl	%ebp
    	jmp	c
    	.p2align 4,,7
    .L10:
    	popl	%ebp
    	.p2align 4,,6
    	jmp	b
    	.p2align 4,,7
    .L9:
    	popl	%ebp
    	jmp	a
    	.size	foo, .-foo
    	.ident	"GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
    	.section	.note.GNU-stack,"",@progbits
    (The code is somewhat odd-looking because of the compiler's tail-call elimination, but you can see that the comparison only happens once)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Quote Originally Posted by Dino View Post
    Or, with the above scenario, is the compiler smart enough to see the multiple compares and generate the code that does a single assembler compare and then branch accordingly?
    Brewbuck proved this. But...

    Quote Originally Posted by Dino View Post
    In assembler to compare two integers, you just compare them. Then, you branch if equal, less than, greater than, or not equal. In C, for example, you compare against EQ, LT, GT, or NE. If I want to test EQ, LT and GT, then that's two compares I have to code. In asm, it would be one compare and then two possible branches. Simpler.
    With higher level languages, comes higher-level expression

    The way boolean logic is expressed in higher-level languages more closely mimics the usual problem domains; i.e you more often than not want to test for one relationship between two values and then operate on whether the test returns true or false.

    Other language constructs can then be used if you need a one-stop comparison. That is, it's fairly easy to emulate assembly on this particular matter. A simple function can return all possible relationships in the form of a integer, for instance.

    In this sense, high-level programming languages aren't selling themselves short by not implementing a similar construct to assembly. Naturally they are if their compilers do not optimize, but that aside and purely at the syntactic level such a construct could easily be deemed unnecessary since it could be implemented by the user, using the regular comparison operators.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You mean like this?

    Code:
    enum Qualean
    {
        True,
        False,
        Maybe,
        MaybeNot
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Or maybe even:

    Code:
    
    enum Quadlean
    {
          False, 
          True,
          Indeterminate,
          Undecided
    };

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    OK, very nice. Thanks brew for the example, and Mario and cp too.

    And actually, this would be quite fun too:
    Code:
    enum Qualean
    {
        True,
        False,
        IfYouAreLucky,
        OnOddThursdays,
        Surprise,
    };
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    The idea however would be something like this:

    Code:
    enum Qualean
    {
        INVALID,
        GT,
        EQ,
        LT
    };
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Reminds me of a Magic 8 Ball... a ton of different, cryptic answers that ultimately only makes you ponder if it's saying 'Yes' or 'No.' Hence why computers have always chosen to reduce everything down to George Boole's flawless logic.
    Sent from my iPad®

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Dino View Post
    OK, very nice. Thanks brew for the example, and Mario and cp too.

    And actually, this would be quite fun too:
    Code:
    enum Qualean
    {
        True,
        False,
        IfYouAreLucky,
        OnOddThursdays,
        Surprise,
    };
    This is more fun -

    Code:
    enum Qualean {
       Blonde,
       Brunnette,
       Redhead,
       Bald
       }
    Last edited by abachler; 11-21-2009 at 01:41 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Grr... not enough states to accomodate those of us with black hair.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    Grr... not enough states to accomodate those of us with black hair.
    You need Quintilean state logic for that and I couldnt think of a one word state name, but there's always room in my heart for a black haired beauty
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by abachler
    I couldnt think of a one word state name
    Yes, blackhead has a very different meaning from redhead!
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    Yes, blackhead has a very different meaning from redhead!
    LOL!!! Maybe you could try the German version "Schwarzkopf" which also means "black head", but maybe it doesn't have the same connotation in German as it does in English?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Awe, this would be my favorite enum list
    Code:
    enum Qualean {
       Redhead,
       Redhead,
       Redhead,
       Redhead
       }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Dino View Post
    Awe, this would be my favorite enum list
    Code:
    enum Qualean {
       Redhead,
       Redhead,
       Redhead,
       Redhead
       }
    Variety is the Spice of life, no way are 4 redheads going to get along with each other unless you find 4 twin sisters
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Page 1 of 2 12 LastLast
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, 11:24 AM
  3. Replies: 4
    Last Post: 04-02-2004, 06:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 09: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

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