lua-users home
lua-l archive

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




On Thu, Oct 24, 2019 at 4:03 PM Tim Hill <drtimhill@gmail.com> wrote:


> On Oct 24, 2019, at 10:52 AM, Phil Leblanc <philanc@gmail.com> 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.

—Tim



Thanks everyone. The C code is the reference implementation for a project so I'll leave it as is. I'm going to drop the Li implementation in Part 1 and move on to building the messages. Thank you Ge for the alternative crcs. I'll keep those around for later.

Oooh, I have a fun project at work. I'mna ask another question...

Russ