Re: Lives there a boolean integer?

Subject: Re: Lives there a boolean integer?
From: "Sandy Harris" <sharris -at- dkl -dot- com>
To: TECHWR-L <techwr-l -at- lists -dot- raycomm -dot- com>
Date: Mon, 06 Mar 2000 14:21:02 -0500

"Mark L. Levinson" wrote:
> I'd like to hear from people who are familiar with
> C and with Perl.

I know C well, but don't use Perl.

> I'm documenting a number of functions
> that return the integer 1 to indicate success and the
> integer 0 to indicate failure. Because these are the
> only two values ever returned, I'm being encouraged
> to refer to them as boolean values, and I'm
> being told that there's no real confusion involved
> because neither C nor Perl supports a true boolean
> data type.

C doesn't, not in the sense that Pascal or other more strongly
typed languages do.

> Should I stand on the principle that an integer
> is not boolean?

I'd use the term Boolean, but be quite careful with it.

If your function returns are being used in C flow control contexts:

if( myfunction() )
while( myfunction() )
for( i = 0 ; myfunction() ; i++ )

Then zero is treated as false and /any non-0 value/ is true. You can
combine these with Boolean operators, ! for not, && for AND, || for OR.
eg. with:

if( functionA() || functionB() )
x = 1 ;
x = 2 ;

x is set to one if either function returns non-zero.
(And functionB() isn't even called if A returns non-zero, but that's

That's one type of Boolean operator in C, but there's another and it
is very different.

C also has bitwise Boolean operations: ^, &, | and ~ for XOR, AND, OR
and NOT respectively, which operate on individual bits of an integer's
binary represenation. so, with four-bit integers, if:

a = 1001
b = 1100
~a = 0110
~b = 0011
a&b = 1000
a|b = 1101
a^b = 0101

As long as you carefully avoid confusing the two classes of Boolean
operations (use the word "bitwise" whenever you talk about the 2nd!),
you should be fine.

Previous by Author: Re: Quality Metrics for Documentation
Next by Author: Re: SUMMARY: Readability studies on fonts--serif and sans serif
Previous by Thread: Lives there a boolean integer?
Next by Thread: RE: Lives there a Boolean integer?

What this post helpful? Share it with friends and colleagues:

Sponsored Ads