Boolean Operators Slighted in C

Gregory Smith greg at utcsri.UUCP
Sun May 4 02:39:13 AEST 1986


In article <778 at bentley.UUCP> kwh at bentley.UUCP (KW Heuer) writes:
>In article <838 at ihwpt.UUCP> ihwpt!knudsen writes:
>>(1) Found out this weekend that you can't say:
>>	boolvar ||= boolean_expression
>
>An interesting expression.  The left side of the assignment would have to be
>evaluated first, and if already set, do nothing.  I presume the reason this is
>omitted is because it's normally written "if (v || e) v=1;" or "if (!v && e)
>v=1;" rather than "v = v || e".
>
Interesting - I was going to suggest if(e)v=1; but that's not the same thing.
I was surprised to find out recently that ` i >> = 1 ' is legal, as opposed to
`>>='. I really think this should produce at least a warning. (4.2BSD)
>
>>(2) There isn't any ^^ (XOR) operator either, as in
>>	if(divisor <0) ^^ (dividend <0)
>>		quotient = -quotient;
>
>There is, but it's spelled "!=".  (Presumably more efficient than "^", though
>I think either one should work with a smart compiler.)

If I had $0.02 for everything that people have said should be done by a smart
compiler... :-) :-) I know, that's what compilers are for.

But it's not quite the same thing. 2 ^^ 1 should be 0, and 2!=1 is 1, and 2^1
is 3. Of course, in the example given, that won't happen. In any case, you
can always say !a != !b.

On the subject of operators, someone said a while back that they had a
compiler with commented-out code to support '/\' and '\/' ( max and min )
operators - and that they had qualms about activating it and then writing
non-portable code. I suggest that they activate it, and then write

#define min(a,b) ((a)\/(b))
#define max(a,b) ((a)/\(b))

in a header, to allow portability. Presumably, the code produced by these
operators should be somewhat better than a>b?a:b. Of course, you still
have to avoid writing min( array[atoi(x1)], i+cindex[getc(file)]) :-).

-- 
"For every action there is an equal and opposite malfunction"
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list