Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Negative shift counts |
| Date | 2015-04-29 20:08 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <mhrdo3$1br$1@speranza.aioe.org> (permalink) |
| References | <mh55g0$20q$1@dont-email.me> |
Noob <root@127.0.0.1> wrote: > 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; The shift operators follow what much hardware does. If you happen to be on a PDP-10, then most likely your negative shifts do what you ask, as that is what the hardware does. Otherwise, it requires a conditional branch on each one, likely much slower than the shift itself. Note also that most hardware uses only some bits of the shift amount. Note that the Fortran ISHFT function does shift left or right based on the sign. Hopefully the compilers optimize for the constant case. -- glen
Back to comp.lang.c | Previous | Next — Previous 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