lua-users home
lua-l archive

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

Dirk Laurie wrote:
2014-03-25 18:20 GMT+02:00 Paul Baker<>:

Regarding the new bitwise operators, the Reference Manual states "Both
right and left shifts fill with zeros the vacant bits". Given that
"Lua 5.3 has integers but not unsigned integers" [1], it seems strange
that the>>  operator is unsigned rather than signed. Is there a reason
for this? Personally, I think it would be useful for Lua to support
both types of right shift - perhaps using the same syntax as
JavaScript:>>  and>>>  ?

Don't agree.

I can see the point of arithmetic shift in C, where it could be useful
when programming multiprecision arithmetic — but C does not have
it. Its inclusion in the bit32 library always felt to me like a loving
preservation of a quirk of some early computer (IBM 360 springs to

I believe that the result of a shift in C must preserve the implied "signedness" of the value being shifted.

For the case of signed values, a right shift will fill high order bits with the value of the MSB.

For unsigned values, a right shift will fill high order bits with 0s

Note, this is untested, but there is plenty of C code that (misguidedly) uses left shift to do division!

If I'm reading the issue correctly, I would expect that right shift would preserve the sign if the default is signed integers. Left shift is free to fill with 0's