Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #6302

Re: Why is shifting too far undefined behvaior?

Path csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From Ike Naar <ike@faeroes.freeshell.org>
Newsgroups comp.std.c
Subject Re: Why is shifting too far undefined behvaior?
Date Tue, 21 Sep 2021 11:33:18 -0000 (UTC)
Organization A noiseless patient Spider
Lines 21
Message-ID <slrnskjgmp.3ce.ike@faeroes.freeshell.org> (permalink)
References <siceh0$1lm$1@solani.org>
Injection-Date Tue, 21 Sep 2021 11:33:18 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="9dcc45fa42a1eb4f5fd60be0ccb897fe"; logging-data="9144"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5DeLNnEToaoJGeNLTsU1/"
User-Agent slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
Cancel-Lock sha1:VsRl49O0vBZVLew/XI4TTRdSy0U=
Xref csiph.com comp.std.c:6302

Show key headers only | View raw


On 2021-09-21, Philipp Klaus Krause <pkk@spth.de> wrote:
> Looking at the C23 draft N2596, section J.2 (but this AFAIK has been the
> same forever), we see that there is undefined behavior, when "An
> expression is shifted by a negative number or by an amount greater than
> or equal to the width of the promoted expression (6.5.7)." or "An
> expression having signed promoted type is left-shifted and either the
> value of the expression is negative or the result of shifting would not
> be representable in the promoted type (6.5.7)."
>
> Does anyone know why this was made undefined (as opposed to yielding an
> unspecified value)? The C99 rationale doesn't mention anything.
>
> The only idea that comes to my mind is that it would be to allow
> implementations to trap at runtime when shifting too far, which might
> help with debugging.

Another thing that comes to mind is that on a machine with N-bit
wordsize, the machine instruction for shifting might only use log2(N) bits
to specify the number of places to shift.
Shifting N places or more would not be possible on such hardware
(at least, not with a single shift instruction).

Back to comp.std.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why is shifting too far undefined behvaior? Philipp Klaus Krause <pkk@spth.de> - 2021-09-21 13:09 +0200
  Re: Why is shifting too far undefined behvaior? Ike Naar <ike@faeroes.freeshell.org> - 2021-09-21 11:33 +0000
    Re: Why is shifting too far undefined behvaior? Richard Kettlewell <invalid@invalid.invalid> - 2021-09-21 12:47 +0100
  Re: Why is shifting too far undefined behvaior? Richard Damon <Richard@Damon-Family.org> - 2021-09-21 08:29 -0400
    Re: Why is shifting too far undefined behvaior? Vincent Lefevre <vincent-news@vinc17.net> - 2021-09-22 01:17 +0000
      Re: Why is shifting too far undefined behvaior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-21 19:05 -0700
        Re: Why is shifting too far undefined behvaior? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-22 00:02 -0400
          Re: Why is shifting too far undefined behvaior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-21 21:12 -0700
            Re: Why is shifting too far undefined behvaior? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-22 09:42 -0400
          Re: Why is shifting too far undefined behvaior? Richard Damon <Richard@Damon-Family.org> - 2021-09-22 07:08 -0400
            Re: Why is shifting too far undefined behvaior? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-22 09:42 -0400
              Re: Why is shifting too far undefined behvaior? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-23 12:18 -0400
          Re: Why is shifting too far undefined behvaior? Vincent Lefevre <vincent-news@vinc17.net> - 2021-09-23 11:10 +0000
            Re: Why is shifting too far undefined behvaior? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-23 12:22 -0400
        Re: Why is shifting too far undefined behvaior? Vincent Lefevre <vincent-news@vinc17.net> - 2021-09-23 11:08 +0000
  Re: Why is shifting too far undefined behvaior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-21 11:01 -0700
    Re: Why is shifting too far undefined behvaior? Philipp Klaus Krause <pkk@spth.de> - 2021-09-21 20:12 +0200
  Re: Why is shifting too far undefined behvaior? Vincent Lefevre <vincent-news@vinc17.net> - 2021-09-22 01:13 +0000
  Re: Why is shifting too far undefined behvaior? David Brown <david.brown@hesbynett.no> - 2021-09-22 14:25 +0200
    Re: Why is shifting too far undefined behvaior? Vincent Lefevre <vincent-news@vinc17.net> - 2021-09-23 11:17 +0000
  Re: Why is shifting too far undefined behvaior? Tim Rentsch <txr@shamko.com> - 2021-10-09 04:27 -0700

csiph-web