lua-users home
lua-l archive

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

> On Oct 24, 2019, at 10:52 AM, Phil Leblanc <> wrote:
> I _think_ (but didn't check or test)  there is an issue in your
> original C code. The function crc_1021_c() and parameter old_crc
> should be declared as _unsigned_ and not _signed_ short integers (ie.
> uint16_t instead of int16_t.  -- parameter 'data' should also be
> unsigned, but it should not make a difference.
> The key issue here is that in C, I _think_ that right shift is
> undefined behavior for signed integers. It is often implemented by
> compilers as an _arithmetic shift_ (ie. a shift "with sign extension"
> -- google arithmetic shift vs. logical shift)

In this case it won’t make any difference, since in all cases the bits shifted “in” from the left are never significant and are discarded (via the various masking steps), so regardless if they are 0 or 1 the code will work. Stylistically, however, I agree that I would code this using unsigned ints to avoid any confusion.