lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Mar 25, 2014 at 11:50 AM, Coda Highland <chighland@gmail.com> wrote:
> On Tue, Mar 25, 2014 at 11:36 AM, Roberto Ierusalimschy
> <roberto@inf.puc-rio.br> wrote:
>> - Formally, even C99 does not have signed shifts:
>>
>>   ISO/IEC 9899:1999 (E)
>>   6.5.7 Bitwise shift operators
>>    4.The result of E1 >> E2 is E1 right-shifted E2 bit positions. [...]
>>      If E1 has a signed type and a negative value, the resulting value is
>>      implementation-defined.
>>
>> (I know that most implementations do signed shifts in that case, but its
>> absence from the standard means something...)
>
> Its absence from the standards meant that C's heritage as a thin
> wrapper around machine language in the 70s is lurking in the name of
> backwards compatibility. It means the standards committee wasn't
> willing to standardize on something that different hardware
> manufacturers disagreed on before I was born. There's a de-facto
> standard in place now -- not only do all of the major C/C++
> implementations use arithmetic (signed) shifts, it IS standardized for
> every other language I've taken the time to look up: Java, C#,
> Javascript, Python, and PHP all agree that right shifts are arithmetic
> shifts.
>
> Having Lua disagree in this respect violates the principle of least surprise.
>
> /s/ Adam

Addendum: Frankly it's easier to synthesize a logical shift out of an
arithmetic shift than the other way around. It's an easier operation
to mask out the high bits after an arithmetic shift than it is to test
the high bits, shift, and then set afterward. The former requires a
single extra operation; the latter requires either a conditional or a
multiplication by a boolean followed by the same extra operation.

/s/ Adam