public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
From: Josh Stone <jistone@redhat.com>
To: Neo Liu <diabloneo@gmail.com>
Cc: SystemTap <systemtap@sourceware.org>
Subject: Re: operator precedence differs from C language
Date: Thu, 01 Apr 2010 17:15:00 -0000	[thread overview]
Message-ID: <4BB4D49D.4010102@redhat.com> (raw)
In-Reply-To: <4BB4C9DC.4040305@redhat.com>

On 04/01/2010 09:29 AM, Josh Stone wrote:
> On 03/31/2010 07:36 PM, Neo Liu wrote:
>> I found that the operator precedence of "&" and "==" differed from C
>> language. Take a look at the following statements.
> 
> That's not true -- in C and in stap, relational operators bind more
> tightly than bitwise operators.  http://tinyurl.com/9q6szp

You might also find this interesting:
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

"""
Neonatal C

Rapid changes continued after the language had been named, for example
the introduction of the && and || operators. In BCPL and B, the
evaluation of expressions depends on context: within if and other
conditional statements that compare an expression's value with zero,
these languages place a special interpretation on the and (&) and or (|)
operators. In ordinary contexts, they operate bitwise, but in the B
statement

    if (e1 & e2) ...

the compiler must evaluate e1 and if it is non-zero, evaluate e2, and if
it too is non-zero, elaborate the statement dependent on the if. The
requirement descends recursively on & and | operators within e1 and e2.
The short-circuit semantics of the Boolean operators in such
`truth-value' context seemed desirable, but the overloading of the
operators was difficult to explain and use. At the suggestion of Alan
Snyder, I introduced the && and || operators to make the mechanism more
explicit.

Their tardy introduction explains an infelicity of C's precedence rules.
In B one writes

    if (a==b & c) ...

to check whether a equals b and c is non-zero; in such a conditional
expression it is better that & have lower precedence than ==. In
converting from B to C, one wants to replace & by && in such a
statement; to make the conversion less painful, we decided to keep the
precedence of the & operator the same relative to ==, and merely split
the precedence of && slightly from &. Today, it seems that it would have
been preferable to move the relative precedences of & and ==, and
thereby simplify a common C idiom: to test a masked value against
another value, one must write

    if ((a&mask) == b) ...

where the inner parentheses are required but easily forgotten.
"""

  reply	other threads:[~2010-04-01 17:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-01  2:37 Neo Liu
2010-04-01 16:29 ` Josh Stone
2010-04-01 17:15   ` Josh Stone [this message]
     [not found]     ` <u2n861674c01004011849o94f1a327za4bc14aff28e3d9@mail.gmail.com>
2010-04-02  2:00       ` Fwd: " Neo Liu
2010-04-01 16:30 ` Frank Ch. Eigler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BB4D49D.4010102@redhat.com \
    --to=jistone@redhat.com \
    --cc=diabloneo@gmail.com \
    --cc=systemtap@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).