[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: CRC16 ccitt 1021 - Part 2
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 24 Oct 2019 17:43:22 -0300
> > int16_t x;
> >
> > x = ((old_crc>>8) ^ data) & 0xff;
> > x ^= x>>4;
> [. . . ]
> 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)
>
> Lua right shift is always a logical shift, without sign extension. So
> the C line " x ^= x>>4;" and the Lua line "x = x ~ (x >> 4)" do not
> compute the same result when 'x' happens to be negative.
After a bitwise-and with 0xff, a 16-bit integer will never be
negative.
-- Roberto