"Interesting" C behaviours

From: Rennie deGraaf (ca.ucalgary.cpsc_at_degraaf)
Date: 11/27/04


Date: Sat, 27 Nov 2004 05:51:20 GMT

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.

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. Mathematically, the congruence classes modulo n are usually
expressed as [0], [1], ... [n-1]. The value -1 would be a member of the
congruence class [n-1]. So, when I'm performing certain operations, I
have to check if a number is negative, and if so, add the modulus to the
residue to get a sensible result. (For example, when subtracting two
struct timevals, and passing the result to select(). select() will barf
if the tv_usec field is negative, at least on Linux, so I have to set it
to (a.tv_usec-b.tv_usec)%1000000, and then add 1000000 if the result is
negative.)

Rennie



Relevant Pages

  • Re: inherent transitivity
    ... How any particular language grammaticalizes them is irrelevant; the English verb "go" is about as intransitive as you can get. ... The term "congruence" was, before the systemic-functionalist linguistics, a perfectly clear term that had a precise use in grammar: two words were in congruence if they shared the same grammatical form, either in some grammatical features or totally. ... the whole idea of separating the expression from the content like this seems both oldfashioned and out of touch with current language typology. ...
    (sci.lang)
  • Re: "Interesting" C behaviours
    ... >the C language, both of which are apparently correct. ... >make any sense to give the bitwise operators lower precedance than ... as the result is known is called "short-circuit behavior". ... and machines without such an instruction require computing ...
    (comp.lang.c)
  • Re: "Interesting" C behaviours
    ... > the C language, both of which are apparently correct. ... > please explain the reasoning behind them? ... > make any sense to give the bitwise operators lower precedance than ... In C99 this was fixed as being towards zero. ...
    (comp.lang.c)
  • Re: Bitwise NOT operator ( ~ )
    ... I've seen the bitwise operators in Flanagan's book and the ECMAScript ... I haven't seen them in any JavaScript programs and haven't used ... language at all given the language was designed for toggling images on ... used server-side and is supported by Windows Script Host. ...
    (comp.lang.javascript)
  • Re: Bitwise NOT operator ( ~ )
    ... I've seen the bitwise operators in Flanagan's book and the ECMAScript ... spec. ... I haven't seen them in any JavaScript programs and haven't used ... language at all given the language was designed for toggling images on ...
    (comp.lang.javascript)