lua-users home
lua-l archive

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


> 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?

There are several reasons:

- In my somewhat limited experience, unsigned shift seems to be much more
common (in useful stuff) then signed shifts.

- Signed shifts can easily be replaced by integer division when needed
(ok, at some performance cost for interpreted Lua).

- Although Lua 5.3 does not have formal unsigned integers, it tries
to accomodate unsigned integers inside integers (e.g., no overflows).

- 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...)

-- Roberto