The value of (a == b) 
Author Message
 The value of (a == b)

I've seen a lot of code using (a == b) type boolean expressions
as a value; now obviously these are 0 when false, but does the
standard define a specific value for them when they are true?
Lots of people assume it's 1.

For example, I've seen code like :

int countCharsSame(char *a,char *b)
{
int count = 0;
        while ( *a && *b )
        {
                count += (*a++ == *b++);
        }
return count;

Quote:
}

If I were doing this I'd put a "? 1 : 0" after the boolean, but
maybe you can convince me that it's redundant and not compiler-
dependent...

------------------------------------------------

--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:
Charles Bloom writes:
> I've seen a lot of code using (a == b) type boolean expressions
> as a value; now obviously these are 0 when false, but does the
> standard define a specific value for them when they are true?
> Lots of people assume it's 1.

They're right.  In the current standard, sections 6.3.3.3, 6.3.8, 6.3.9,
6.3.14, and 6.3.15 guarantee# that the following 9 operators will always
return 0 or 1:   !  !=  <  <=  >  >=  ==  &&  ||

The place where "true" return values are NOT guaranteed to be 1 is with
the <ctype.h> character classification functions such as isalpha(),
isprint(), and isdigit().  For these functions, section 7.3.1 guarantees#
only that they will return 0 or nonzero as appropriate.

All this is the same in the new draft standard, only the section number
references are different.

# - Except, of course, if you have done anything to cause undefined
behavior.  In that case anything can happen, and something bad probably
will, though probably not anything to do with these particular guarantees.
--
Mark Brader, Toronto  | Do not meddle in the affairs of undefined behavior,

My text in this article is in the public domain.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)

says...

Quote:

> I've seen a lot of code using (a == b) type boolean expressions
> as a value; now obviously these are 0 when false, but does the
> standard define a specific value for them when they are true?

The standard requires that the built-in comparison operators return 0
or 1 to indicate false or true respectively.  In other areas, such as
the functions/macros in ctype.h, that's not the case though: they can
return any non-zero value to indicate true.

Quote:
> Lots of people assume it's 1.

They're justified in doing so.  Section 6.3.8, 6.3.9, 6.3.13 and
6.3.14 specify that the relation, equality, logical AND and logical OR
operators respectively, return 0 or 1 as their results.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)

# I've seen a lot of code using (a == b) type boolean expressions
# as a value; now obviously these are 0 when false, but does the
# standard define a specific value for them when they are true?
# Lots of people assume it's 1.

You can rely on that. The Standard says that the result of relational
operators (<, >, <=, >=), equality operators (==, !=), and logical
operators (&&, ||) is 0 when false and 1 when true and has type int.

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh/
SIGSIG -- signature too long (core dumped)
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)


Quote:

> I've seen a lot of code using (a == b) type boolean expressions
> as a value; now obviously these are 0 when false, but does the
> standard define a specific value for them when they are true?
> Lots of people assume it's 1.

Section 3.3.8 of the ANSI C standard says, in part, under the
`semantics' subheading:

  Each of the operators <, >, <=, and >= shall yield 1 if the specified
  relation is true and o if it is false.  The result has type int.

Section 3.3.9 says, in part:

  The == and != operators are analagous to the relational operators
  except for their lower precedence.  Where the operands have types
  and values suitable for the relational operators, the semantics
  detailed in 3.3.8 apply.

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)
[This followup was also e-mailed to the cited author for speed -- please
follow up in the newsgroup.]


Quote:

>I've seen a lot of code using (a == b) type boolean expressions
>as a value; now obviously these are 0 when false, but does the
>standard define a specific value for them when they are true?
>Lots of people assume it's 1.

And with good reason.

"Each of the [relational] operators ... shall yield 1 if the specified
relation is true and 0 if it is false." -- ANSI 3.3.8/ISO 6.3.8

ANSI 3.3.9/ISO 6.3.9 adds that the equality operators == and != share the
same semantics where the operands have suitable types and values.

--
There's no need to e-mail me a copy of a follow-up; but if you do,
please identify it as such.

Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                                    http://www.mindspring.com/~brahms/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)


Quote:

> I've seen a lot of code using (a == b) type boolean expressions
> as a value; now obviously these are 0 when false, but does the
> standard define a specific value for them when they are true?
> Lots of people assume it's 1.

It is 1. I don't have the standard handy, but it's defined to be 1.

..bit of an odd choice, that. Why not -1? All zeroes, for false, all
ones, for true?

--
+- David Given ---------------McQ-+ "[SF is not about predicting the future.]


+- http://wired.st-and.ac.uk/~dg -+ Stephen Dedman                            
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)

   I've seen a lot of code using (a == b) type boolean expressions
   as a value; now obviously these are 0 when false, but does the
   standard define a specific value for them when they are true?
   Lots of people assume it's 1.

It's always 1.  So sayeth the Standard, and so mote it be.

--
"To the engineer, the world is a toy box full of sub-optimized and
 feature-poor toys." --Scott Adams
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)


Quote:

>I've seen a lot of code using (a == b) type boolean expressions
>as a value; now obviously these are 0 when false, but does the
>standard define a specific value for them when they are true?
>Lots of people assume it's 1.

I don't have a copy of the standard, but according to H&S3, section
7.6.5, _Equality_Operators_:

   "The result is always of type |int| and has the value 0 or 1."

(The vertical bars indicate |courier font|)

Regards,

                          -=Dave
Just my (10-010) cents
I can barely speak for myself, so I certainly can't speak for B-Tree.
Change is inevitable.  Progress is not.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Thanks for all the replies...


Quote:

>It is 1. I don't have the standard handy, but it's defined to be 1.

>..bit of an odd choice, that. Why not -1? All zeroes, for false, all
>ones, for true?

Yeah, that's what threw me off.  Most of the POSIX functions return
(-1) for true (though they just gaurantee non-zero) in compilers I use.

Oh well.. I'll use "a += (a==a)" to increment variables from now on :^)

------------------------------------------------

--



Mon, 12 Nov 2001 03:00:00 GMT  
 The value of (a == b)
[This followup was also e-mailed to the cited author for speed -- please
follow up in the newsgroup.]


Quote:
>It is 1. I don't have the standard handy, but it's defined to be 1.

>..bit of an odd choice, that. Why not -1? All zeroes, for false, all
>ones, for true?

I suspect it's defined to be 1 so that
        (a <= b)
gets the same result as
        !(a > b)

The ! operator is defined to return 0 or 1.

--
There's no need to e-mail me a copy of a follow-up; but if you do,
please identify it as such.

Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                                    http://www.mindspring.com/~brahms/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
--



Mon, 12 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:

> It's always 1.  So sayeth the Standard, and so mote it be.

*nods* This is correct. If you think I am contradicting myself below, you
have either misread it, or I am confused.

However... The return-zero-or-one property of boolean operators is only
relevant when used in an integer context. The compiler may optimise to
whatever form convient when the zero-or-one property is not used.

For example;

if (a==b)
{
        /* blah */

Quote:
}

The compiler may look at the if expression and produce assembly to perform
a CMP and then BNE over the "then" block. No intermediate value which has to
be zero-or-one is neccessary.

I prefer to think that boolean operations produce data items of a hidden
"boolean" type. The boolean data type can store true or false in any
means convienient to the compiler and the CPU, however, when data of this
type is used in an integer context, it must yeild a zero or a one value.

The above isn't strictly true. Boolean operators return integers of zero
or one values, and compilers are allowed to optimise.

My concept of a temporary boolean data type works for all the situations I
can think of, and I'm sure someone out there can think of an exception.
(Anyone?)

However... Having said all that, there is another optimisation a compiler
can get away with. If a local integer variable is only ever used in a
boolean context, the compiler could get away with ignoringly the true-is-one
rule and using a convinient value.

Eg.

int bool,a;
a=some_function();
bool=(a!=28);
/* Lots of stuff, bool is not accessed. */
if (bool)
        /* blatch */

Here, the compiler can get away with using any convienient value for bool.
A XOR operation between a and 28 would be handy, and the result of the
XOR simply stored in a. This value may not be a a one, but here, we don't
care. It's another possibility for optimisation.

At any point, if you pass bool to a function, assign it, or use it in an
arithmetic context, or any other way that the zero-or-one property becomes
important. The XOR instruction would have to be followed by an operation
to reduce any non-zero value in the relevant register down to one.

Bill, yes, has no bananas.
--



Fri, 23 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:

> Oh well.. I'll use "a += (a==a)" to increment variables from now on :^)

I can't work out whether or not that's legal.  The standard says:

| Between the previous and next sequence point an object shall have its
| stored value modified at most once by the evaluation of an expression.
| Furthermore, the prior value shall be accessed only to determine the value
| to be stored.

Does the (a==a) constitute an access which will "only determine the value to
be stored"?

--

 ** Please send on-topic followups by Usenet, not email **
--



Fri, 23 Nov 2001 03:00:00 GMT  
 The value of (a == b)

Quote:



> > I've seen a lot of code using (a == b) type boolean expressions
> > as a value; now obviously these are 0 when false, but does the
> > standard define a specific value for them when they are true?
> > Lots of people assume it's 1.

> It is 1. I don't have the standard handy, but it's defined to be 1.

> ..bit of an odd choice, that. Why not -1? All zeroes, for false, all
> ones, for true?

All ones is only -1 if the representation used for negative integers is
two's-complement. The c standard doesn't specify that it has to be so.

--
{ Sunil Rao }
"In my opinion, I think that an author when he is writing should
 definitely not get into the habit of making use of too many
 unnecessary words that he does not really need in order to put his
 message across."  - George L Trigg, Phys. Rev. Lett. 42748 (1979).
--



Fri, 23 Nov 2001 03:00:00 GMT  
 The value of (a == b)


   > It's always 1.  So sayeth the Standard, and so mote it be.

   *nods* This is correct. If you think I am contradicting myself below, you
   have either misread it, or I am confused.

   However... The return-zero-or-one property of boolean operators is only
   relevant when used in an integer context. The compiler may optimise to
   whatever form convient when the zero-or-one property is not used.
   [...]

Yup, definitely.  This is a special case of the general rule that the
compiler may actually generate code to do anything it wants, as long
as the effects are the same.
--
"To the engineer, the world is a toy box full of sub-optimized and
 feature-poor toys." --Scott Adams
--



Fri, 23 Nov 2001 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Visual C++ Developer ( Bs. As. Argentina )

2. comp bs

3. Borland Support: JD Powers AD is B.S.

4. Value, value, who's got the value?

5. Resetting PropertyGrid value to old value

6. addition long values to long double value ?

7. global value and extern value

8. How can I transform a int value to a char value

9. read WMF-header and convert DWORD value to integer value

10. l-value and r-value

11. ~Sum existing values with values from another table

12. Simple C++ question: Using return values (by value)

 

 
Powered by phpBB® Forum Software