Dennis Ritchie wrote about this in comp.arch in 2002, but didn't
specifically explain why it's undefined behavior rather than an
unspecified value.
https://yarchive.net/comp/c_shifts.html
From: Dennis Ritchie <
d...@bell-labs.com>
Newsgroups: comp.arch
Subject: Re: shift instructions on different processors
Date: Tue, 12 Feb 2002 04:47:05 +0000
Message-ID: <
3C689E49...@bell-labs.com>
glen herrmannsfeldt wrote:
>
> There have been questions on both C and Java newsgroups about
> the effect of shift operations when the shift value equals or
> exceeds the number of bits available to be shifted.
>
> I know that the reason for such a restriction is that many architectures
> use only some bits of the shift amount.
>
> x86 uses the low 5 bits for 32 bit shifts, and low 6 bits for 64 bits.
>
> IBM S/360, S/370, S/390 etc., use the low 6 bits for 32 and 64 bits.
...
Meissner followed up with various other architectures as well.
I looked at my Interdata 8/32 manual (1975), which describes
a 32-bit machine, and was amused to find, under the
"Shift Left Logical" instruction,
"... the shift count is specified by the least significant
five bits of the second operand."
I added a contemporaneous hand-written notation "!!" to this.
On the next page, under "Shift Right Logical," it similarly
says, "the least significant five bits of the second operand."
Here my notation is an underlining, accompanied by "Shit!!"
And that, children, is why the C and Java rules are as they are.
The C manual in 6th Edition Unix didn't have the value restriction.
K&R I did.
Dennis
Perhaps it was thought that shifting too far is analagous to integer
overflow, which has undefined behavior for signed types. And Ritchie
may not have wanted to assume that no CPUs trap on a large shift.
K&R1 says "The result is undefined", not that it has undefined behavior
(I don't think K&R1 had the concept of undefined behavior).
--
Keith Thompson (The_Other_Keith)
Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */