"christian.bau" wrote:
>
> David T. Ashley wrote:
>
> > Wow! I've never seen this behavior. I would have assumed that shifting
> > something too many times to the left always gets you zero and to the right
> > either gets you zero or -1.
>
> On IA-32 processors (Pentium, Athlon etc. ), a hardware shift
> instruction uses only the lower five bit of the shift count, so (x <<
> n) produces the same result whether n == 32 or n == 0.
If it used all 32 bits of the "shift by" value, imagine how long
a 4 billion bit shift would take.
(The only other option being to check that the value had non-zero
bits in the upper part, and force zero/negative-one as the result
immediately. However, as long as it's documented, the current
behavior of only using the low 5 bits makes sense from both a
hardware and software point of view.)
Getting back to C, this is probably the exact reason why shifts of
larger than the value is undefined. (Though "implementation
defined" may have been more appropriate, I suppose there may be
hardware in which the results are undefined at the hardware level.)
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>