lua-users home
lua-l archive

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

On Mar 26, 2014, at 5:01 AM, Roberto Ierusalimschy <> wrote:

>> As a side note, arithmetic shift is NOT equivalent to division by a power of two as the two functions have different rounding properties for negative dividends. Assuming you are using a C compiler that *does* do arithmetic shifts with signed operands, the following code will simulate integer division using shifts only:
> This is in C. In Lua both are equivalent; integer division in Lua rounds
> toward minus infinity, like an arithmetic shift.
> -- Roberto

Interesting, and a bit worrying. Is there any reason for this? I suspect this is going to be a source of confusion for some people porting code from C, as this can introduce subtle off-by-one bugs in algorithms.