Re: "Interesting" C behaviours

From: Chris Torek (nospam_at_torek.net)
Date: 11/27/04


Date: 27 Nov 2004 06:44:25 GMT

In article <sBUpd.360758$%k.84068@pd7tw2no>
Rennie deGraaf <ca.ucalgary.cpsc@degraaf> wrote:
>In the last few days, I have discovered two "interesting" behaviours of
>the C language, both of which are apparently correct. Could someone
>please explain the reasoning behind them?
>
>1. The operators '^', '&', and '|' have lower precedance than '==',
>'!=', '>=", etc. I discovered this when the statement "if (array1[i] ^
>array2[i] == 0xff)" failed to do what I expected. To me, it doesn't
>make any sense to give the bitwise operators lower precedance than
>comparators. I can't see any situation where someone would want to
>perform a bitwise operation on a truth value, but that is what the
>language specifies for the above expression.

Dennis Ritchie has noted that, in Primeval C, there were no "&&" and
"||" operators at all, and the single "&" and single "|" operators
were overloaded, so that:

    result = f() | g();

would call both f() and g(), and bitwise-OR the results together to
store in the variable "result", while:

    if (f() | g())

would call f() first, and if the result was nonzero, would omit the
call to g() entirely (i.e., what || does now). Likewise:

    result = f() & g();

always called both, while:

    if (f() & g())

called g() only if f() returned a nonzero value. Stopping as soon
as the result is known is called "short-circuit behavior". Presumably
the operators' priorities were set based on the short-circuit
versions, which probably occurred more often.

At some point, it was deemed bogus to have a single operator for
two entirely separate meanings, so the short-circuit logical
operators were separated out into new && and || operators. But by
then there were perhaps a few dozens of kilobytes :-) of source
code, so the operator parsing was left unchanged.

>2. In C99, the expression 'a%b' where a<0 and b>0 will return a negative
>result (K&R lets this be machine dependent). While it is technically
>correct (-1 is congruent to 6 mod 7), it isn't exactly what most people
>expect.

I think you have overly high expectations of "most people" here. :-)

>Mathematically, the congruence classes modulo n are usually
>expressed as [0], [1], ... [n-1].

True; but the "%" operator is really the "remainder after division"
operator. The goal is to have (a/b + a%b) == a, whenever b != 0.
To have ((-3) % 7) == 4, we would have to have ((-3) / 7) == -1),
but 0 is the most common result today for this division. Thus,
machines with "remainder after divide" instructions mostly produce
-3 here, and machines without such an instruction require computing
(a % b) via (a - (a / b) * b) in the first place.

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: New Cobol compiler written in Cobol
    ... machines at the time were the IBM 7090 and similar 7040. ... the 14xx line had grown to become the dominant business computer. ... Programmers won't put up with cryptic languages like C++ ... They'll use a language they can understand at a glance. ...
    (comp.lang.cobol)
  • Re: What does the future of computing hold?
    ... sophisticated views about boiling language down or not. ... deterministic physical machines, or do you follow Turing and think, ... We may have metaphysical souls, but the fact that computers are lousy at ... For ethical reasons, you cannot grow human babies to ...
    (comp.programming)
  • Re: What does the future of computing hold?
    ... sophisticated views about boiling language down or not. ... deterministic physical machines, or do you follow Turing and think, ... We may have metaphysical souls, but the fact that computers are lousy at ... humans are. ...
    (comp.programming)
  • Re: I wonder what the "Science" is in this Newsgroup
    ... I expanded a French word into English when I called Standford ... my definition of language contradicts your opinion ... Bacteria talk, trees talk, machines won't talk. ...
    (sci.lang)
  • Re: Is the Curt net a kind of decision tree?
    ... eye of the human beholder here, ... This is how we use language to gain all our understanding of ... Gravity is in the eye of the beholder. ... will best help us solve the problem of building intelligent machines. ...
    (comp.ai.philosophy)