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

"Thomas R. Truscott" <trt@cs.duke.edu>
2 Jul 2002 01:16:05 -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: "Thomas R. Truscott" <trt@cs.duke.edu>
Newsgroups: comp.compilers
Date: 2 Jul 2002 01:16:05 -0400
Organization: SAS Institute Inc.
References: 02-06-079
Keywords: syntax, design
Posted-Date: 02 Jul 2002 01:16:03 EDT

> ... 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: ...


However the language does it, programmers will occasionally do it wrong.
The gcc compiler issues a helpful warning:


        suggest parentheses around + or - inside shift


Another mistake I have seen is "if (x << y)" when the programmer
meant "if (x < y)". (Vanilla gcc does not warn about that.)


One can try to design the language to reduce mistakes.
To avoid the 1+2<<3 problem, require parentheses when mixing operators.
To avoid the if (x << y)" problem, require "boolean" values in the if.
I think language designers (and standards people) prefer this to warnings,
perhaps because they see warnings as vague and "non-normative".
But this is just converting warnings into mandatory errors.
I think mandatory warnings would also be appropriate.


Tom Truscott


Post a followup to this message

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