More on BOOLEAN vs. boolean

HARGED%ti-eg.csnet at CSNET-RELAY.ARPA HARGED%ti-eg.csnet at CSNET-RELAY.ARPA
Tue May 6 07:46:23 AEST 1986


>(2) There isn't any ^^ (XOR) operator either, as in
>	if(divisor <0) ^^ (dividend <0)
>		quotient = -quotient;

Periodically I encounter conditional expressions in the form

	((a && !b) || (!a && b))

For those who remember their Karnaugh maps, that is equivalent to

	(a ^^ b)

I normally grit my teeth and use "^", including a note specifying that the
variables involved had *better* always be 1 or 0.

>(3) There is no boolean data type.  No big gripe;
>lots of us say "typedef short bool" in our .h files.

When I first started using C, I thought I would miss the boolean type. I'll
have to admit that I haven't. With a (very) modest amount of discipline, a 
BOOLEAN can be typedef'ed, and all else remains the same.

>o  Multiple flag variables with local scope and no address operator (e.g.
>   variables declared "register bool") could be packed into a single word.

*IF* packing bits is feasible on your host machine. (Whether you like it or
not) there are many C implementations that can't afford the overhead of bit
packing. 

>o  "++x" and "--x" could be defined as "set" and "clear"; "x++" and "x--"
>   would then be "test and (set|clear)".  This would obviate such things as
>   "if (!flag) { flag=1; printmsg(); }".

I think the same problems with doing that now (on integers of whatever size)
would plague the boolean type. The code that would have to generated to avoid
giving a boolean variable an undefined value would probably match the size
of the code currently generated when the user explicitly controls a flag's
value. (x=true) and (x=false) for "set" and "clear" seem more than adequate. 
However, (~x) for "toggle" would be useful. The lack of an efficient
toggle operation on integer BOOLEAN's is sometimes a pain.

You are aware that the short-circuit evaluation rules associated with || and
&& actually make them shorthand forms for

	(a || b) <==> (a ? 1 : (b != 0))
	(a && b) <==> (a ? (b != 0) : 0)

which might have something to do with their not being included in the set
of operators that can be combined with the assignment operator.

Richard Hargrove			csnet: harged at ti-eg
Texas Instruments, Inc			arpa : harged%ti-eg at csnet-relay.arpa
----------------------			------------------------------------



More information about the Comp.lang.c mailing list