# Why only boolean? Why not quadlean?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-19-2009
Dino
Why only boolean? Why not quadlean?
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 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?
• 11-19-2009
brewbuck
Quote:

Originally Posted by Dino
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)
• 11-19-2009
Mario F.
Quote:

Originally Posted by Dino
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
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.
• 11-20-2009
cpjust
You mean like this?

Code:

```enum Qualean {     True,     False,     Maybe,     MaybeNot };```
• 11-20-2009
Sebastiani
Or maybe even:

Code:

``` enum Quadlean {       False,       True,       Indeterminate,       Undecided };```
:p
• 11-20-2009
Dino
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, };```
• 11-20-2009
Mario F.
The idea however would be something like this:

Code:

```enum Qualean {     INVALID,     GT,     EQ,     LT };```
• 11-20-2009
SlyMaelstrom
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.
• 11-21-2009
abachler
Quote:

Originally Posted by Dino
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   }```
• 11-21-2009
laserlight
Grr... not enough states to accomodate those of us with black hair.
• 11-21-2009
abachler
Quote:

Originally Posted by laserlight
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 ;)
• 11-21-2009
laserlight
Quote:

Originally Posted by abachler
I couldnt think of a one word state name

• 11-21-2009
cpjust
Quote:

Originally Posted by laserlight

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? :)
• 11-21-2009
Dino
Awe, this would be my favorite enum list
Code:

```enum Qualean {   Redhead,   Redhead,   Redhead,   Redhead   }```
• 11-21-2009
abachler
Quote:

Originally Posted by Dino
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 :D
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last