What precedence for shift-left/shift-right operators?

"Timon Christl" <christl@bandos.fmi.uni-passau.de>
28 Jun 2002 18:13:45 -0400

          From comp.compilers

Related articles
What precedence for shift-left/shift-right operators? christl@bandos.fmi.uni-passau.de (Timon Christl) (2002-06-28)
Re: What precedence for shift-left/shift-right operators? trt@cs.duke.edu (Thomas R. Truscott) (2002-07-02)
Re: What precedence for shift-left/shift-right operators? christl@belinda.fmi.uni-passau.de (Timon Christl) (2002-07-04)
| List of all articles for this month |

From: "Timon Christl" <christl@bandos.fmi.uni-passau.de>
Newsgroups: comp.compilers
Date: 28 Jun 2002 18:13:45 -0400
Organization: Compilers Central
Keywords: syntax, design
Posted-Date: 28 Jun 2002 18:13:44 EDT

Most languages with a C like expression syntax (this includes C, C++,
Java, Perl, Ruby and a few others) define the precedence of <<
and >> (bit-wise shift-left and shift-right) different than the
multiplicative operators * / %, but at least one language (Object
Pascal) defines them to be the same.

I'm confused about this, as this means that for example 1+2+3+4<<2 means
"sum up 1, 2,3 and 4, then shift it left two times". I find the Pascal
way more intuitive: "Sum up 1, 2, 3, and the result of 4 shifted left 2
times" because it is closer to 1+2+3+4*4 (Remind that a shift is just a
multiplication by 2).

What do you think? Which alternative would you prefer? This is purely a
design question and I would like to hear different opinions before
deciding which precedence to use in my toy language.

Timon Christl <me@christltimon.de>
[Whatever you're used to seems "intuitive". In practice, few of us can
remember all of the precedence rules so we overparenthesize to make our
intent clear. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.