Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Negative shift counts |
| Date | 2015-04-21 12:02 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <874mo968wu.fsf@bsb.me.uk> (permalink) |
| References | <mh55g0$20q$1@dont-email.me> |
Noob <root@127.0.0.1> writes: > I am aware that negative shift counts have undefined behavior. > > int i = 40 << -2; // UB > > It seems to me that a natural way to extend shift counts to > negative values would be to just flip the shift direction. > > The above statement would behave "as if" we'd written > > int i = 40 >> 2; > > > I suppose negative shift counts were unsupported in the original > language definition (?) in the 70s and remained unsupported in the > first edition of K&R C? Perhaps this was done to save space when > RAM was tight, and such a "feature" was not considered to bring > any value? The original view of C was that it should not offer very much over and above what the machine offers. The structuring (blocks, ifs, loops etc.) is no more than thin syntax over tests and jumps. Functions in C can't be nested because most machines offer little more than stack pointer relative addressing. A higher-level shift would require (on some systems) code to do checks every time, except in those probably rare cases when the compiler can optimise 40 << -2 into 40 >> 2. That's the sort of universal cost that C tries to avoid. > Then ANSI saw no benefit to discuss defining the behavior during > the standardization process in the 80s? Because no implementation > offered this as an extension? > > I'm perplexed, because it seems to be a trivial extension, with > low complexity, for a small gain in expressiveness. It certainly simple (low complexity) but it's not zero-cost, and the cost has to be set against with the ease with which an inline function can be written to do the job. <snip> -- Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Negative shift counts Noob <root@127.0.0.1> - 2015-04-21 11:33 +0200
Re: Negative shift counts David Brown <david.brown@hesbynett.no> - 2015-04-21 12:22 +0200
Re: Negative shift counts Noob <root@127.0.0.1> - 2015-04-21 13:12 +0200
Re: Negative shift counts Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-21 12:02 +0100
Re: Negative shift counts glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-29 20:08 +0000
csiph-web