Author Topic: Boolean concept  (Read 16278 times)

Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Boolean concept
« on: June 08, 2012, 10:18:45 PM »
Hello, I have been wondering about the concept of how booleans work and I was wondering how it works in assembly.

My perceptive is with high-level languages so that's how I can present my question.  Hopefully you can understand what I'm talking about.

Here are different ways that booleans can be used:

If int x is 0, it's FALSE.
If int x is 5 it's not 1 but it's still TRUE.
Yet, I can say bool myBool = TRUE    <---- in the above line TRUE is not an exact value (it could be 1 or 5 or anything), but now we are treating it as an exact value (1).


I just don't get it.  So I guess my question is, what IS a boolean and how does it work?


Also, is there such thing as a boolean "value" or not?

bool mybool = TRUE seems to be a variable that stores a boolean value, which is presumably a single-bit number (not counting whether it's padded or aligned to more bits under the hood).

Yet in something like this: bool myBool = (5 == 6), the end part: (5 == 6) doesn't seem to me like it's a value at all, but more of a truth statement.  How is the truth statement be converted into a variable?

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Boolean concept
« Reply #1 on: June 08, 2012, 11:10:00 PM »
Here are different ways that booleans can be used:

If int x is 0, it's FALSE.
If int x is 5 it's not 1 but it's still TRUE.
Yet, I can say bool myBool = TRUE    <---- in the above line TRUE is not an exact value (it could be 1 or 5 or anything), but now we are treating it as an exact value (1).

In C, true and false are defined as:
Code: [Select]
#define FALSE 0
#define TRUE (!(FALSE))

Or something similar. That means your view there is kinda wrong. Technically TRUE is the maximum unsigned value for the defined type. What does that mean? Well, if we have an 'int' that is defined as 4 bytes wide, the value of TRUE would be 0xFFFFFFFF. This is the reason that TRUE is not an exact value. it's value depends on the types size. For this integer it's value in decimal is (4,294,967,295) whereas if you would have defined that type to be a 'char' of 1 byte in size, it would be 0xFF or (255).

I just don't get it.  So I guess my question is, what IS a boolean and how does it work?


Also, is there such thing as a boolean "value" or not?

As I explained above, not really. But the concept is a good thing to know. Despite what I posted up there about boolean values being the maximum unsigned value, in high level languages you can still do things like:

Code: [Select]
if (x == TRUE) {...}
And if 'x' is 5, it'll execute. So why? This is because the comparison being done is against FALSE (in actuality). In assembly, we'd compare 5 against 0 and check the zero flag (ZF) in the status word. If ZF isn't set, then 'x' isn't false, so we assume it's TRUE.

Code: [Select]
if_stmt:
   cmp dword [x], 0
   jz .end_if
      ;....
.end_if:

bool mybool = TRUE seems to be a variable that stores a boolean value, which is presumably a single-bit number (not counting whether it's padded or aligned to more bits under the hood).

Yet in something like this: bool myBool = (5 == 6), the end part: (5 == 6) doesn't seem to me like it's a value at all, but more of a truth statement.  How is the truth statement be converted into a variable?

That's up to the compiler to decide. A lot of compilers will use the machine's word-size as the 'boolean type', but this isn't something set in stone. From the C programmer's point of view, it's a "concept" provided by the compiler, not an actual implementation.

Hope this helped...

About Bryant Keller
bkeller@about.me

Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Re: Boolean concept
« Reply #2 on: June 13, 2012, 08:00:59 PM »
This clears it up for me really well, thank you very much for explaining that. So you never really compare if X is "false"; you just compare if X is "zero" (and "zero" could be 0x00 or 0x0000 or 0x00000000 or whatever).

And the other part that was confusing me which how you can evaluate whether X == TRUE, even though X might be 5 instead of 1.  So thanks for your macro example #define TRUE (!FALSE).  I think I understand it now.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Boolean concept
« Reply #3 on: June 13, 2012, 09:37:42 PM »
When coding in C I always use either:
Code: [Select]
   if ( X == 0 )  // ( X == false )
- or -
Code: [Select]
   if ( X != 0 )  // ( X != false )
to guarantee myself the compiler doesn't bork my code and assembles to the quickest jz/jnz opcodes.
I've seen TRUE defined multiple ways but FALSE is always defined as zero.  It can become especially heinous when using headers from import libraries from 3rd parties that set "their" version of TRUE differently causing if ( X == TRUE ) to fail in some cases!

C++ has the keywords true / false but there's still tons of old code to be maintained.

You might reference the articles http://milsecure.org/article.php?story=Boolean-values-are-evil or http://www.jroller.com/alexRuiz/entry/boolean_arguments_can_be_evil to see what I'm talking about.

That's just the tip of the iceberg...

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Boolean concept
« Reply #4 on: June 14, 2012, 12:07:47 AM »
It can become especially heinous when using headers from import libraries from 3rd parties that set "their" version of TRUE differently causing if ( X == TRUE ) to fail in some cases!

So... truth is relative after all ;D

Offline Ux

  • Jr. Member
  • *
  • Posts: 12
Re: Boolean concept
« Reply #5 on: August 01, 2012, 12:46:06 AM »
In assembly, I can't speak for others but my impression is there are two basic conventions.

False is always 0, but true is either -1 or 1.

For the 1 case, let's say I want to set a boolean:
   cmp edx, 100
   setle al
This sets al to 1 if edx is <= 100, and 0 if it's not.

For the -1 case, let's say I care only about the carry.
   cmp edx, 100
   sbb al, al
If edx is less than 100, al is set to -1, else 0.

But for testing whether a value is true = nonzero, when the boolean result is discarded, I just do an OR.
   or edx, edx
   jnz .L1000   ; jump if edx is true.

Voila.
« Last Edit: August 01, 2012, 12:47:37 AM by Ux »