Boolean Operators Slighted in C

Root Boy Jim rbj at icst-cmr
Wed May 14 11:08:22 AEST 1986


	Doug Gwyn responds:
	Steven Brian McKechnie Sargent <valid!sbs> says ("> ..."):
	> A Boolean data type distinct from integers clutters the language with
	> another (moderately useless) type and removes handy idioms like
	> 	foo[x == 0] = bar;
	
	Boolean quantities form an important abstract data type distinct from
	counters, sets, real numbers, etc.  By not making the distinction,
	the language encourages usage errors, the most notorious being
		if ( a = b )
			stuff;

True. It is easily found tho.

	Sure, one can make integers do double (or triple, or ...) duty,
	but by now every programmer should realize the value of data
	abstraction.
	
Or lack of it. I find that the Boolean/Integer schism in Fortran caused
me lots of headaches long ago.

	> New operators like "< =", in addition to being hard to spell and
	> understand,
	> facilitate usages that just aren't that common.  I hardly ever want to
	> say x = (x < y), and I don't mind typing the extra characters. I'm not
	> interested in paying compiler vendors money for supporting this
	> (or any other dubious misfeature pounded into the language for
	> "completeness'sake.")
	
	Here is an example where proper use of Boolean data would be violated.
	A statement like
		x = x < y;
	should NEVER be used, since it assigns a Boolean result to an
	arithmetic variable.  This means that a "< =" operator is not
	a good idea.
	
Wrong you are, Doug. You have been thinking in this Boolean paradigm
for so long that you are missing another Good thing. Implication!
Given that P and Q are `Boolean' variables (0 or 1), the Logical
Implication is described by the operator `<='. Thus, the statement
`x = p <= q;' sets the `boolean' value x to a `boolean' expression.
Since there are no such things as booleans :-), x, p, & q are integers.
It's all a matter of interpretation. APL has no `boolean exclusive or'
operator because the operator `!=' does the same thing (when given
only zero's or one's). There are sixteen different boolean functions
with two inputs and one output. They all fit nicely into C operators:

	p 0 0 1 1
	q 0 1 0 1	Name		Symbol
	---------	----		------
	  0 0 0 0	False		0
	  0 0 0 1	And		&
	  0 0 1 0	Doesn't Imply	>
	  0 0 1 1	Alpha		p
	  0 1 0 0	Not Implied By	<
	  0 1 0 1	Omega		q
	  0 1 1 0	Diff		!= or ^
	  0 1 1 1	Or		|
	  1 0 0 0	Nor		!(p | q)
	  1 0 0 1	Same		==
	  1 0 1 0	Not Omega	!q
	  1 0 1 1	Is Implied By	>=
	  1 1 0 0	Not Alpha	!p
	  1 1 0 1	Implies		<=
	  1 1 1 0	Nand		!(p & q)
	  1 1 1 1	True		1

Neat! I never thought I'd get to use my engineering switching theory again!

	I find data abstraction to be a Good Thing, and have produced
	much better code since defining a Boolean data type and using
	it in a strictly type-correct manner.
	
This fact doesn't bother people who write in APL, widely considered
to be the most mathematical language. Why does it bother you?

	(Root Boy) Jim Cottrell		<rbj at cmr>
	"One man gathers what another man spills"



More information about the Comp.lang.c mailing list