RE: [Boost-Users] RE: Re: CRC problem with 8 bit truncated polyno mial
Vladimir Prus wrote:
Sorry, I don't understand what you're saying...
std::cout << "Optimal CRC-8 : " << std::hex << (unsigned)crc_8() << std::endl;
is the same as
std::cout << "Optimal CRC-8 : " << std::hex << (unsigned int)crc_8() << std::endl; Sorry, I wasn't very clear in my last message.
The results can be unexpected, depending on whether the result of crc_8() (or more precisely, operator() ) is signed or unsigned. If operator() returns an unsigned value, there will be no problems, and the output will be ea. On the other hand, if operator() returns a signed value, then it will first be promoted to a signed int, then converted to an unsigned int. Using the original example values posted by Victoria, the output would be ffffffea. In this scenario, the return value must be first cast to an unsigned char, then to an unsigned int: ... << static_cast<unsigned>( static_cast<unsigned char>( crc_8() ) ) I just had a quick look at the crc.hpp file, and operator() returns an unsigned value, thus banishing my caveat to the dusty corners where all academic footnotes end up ;-) -- Jim
Jim.Hyslop wrote:
Vladimir Prus wrote:
Sorry, I don't understand what you're saying...
std::cout << "Optimal CRC-8 : " << std::hex << (unsigned)crc_8() << std::endl;
is the same as
std::cout << "Optimal CRC-8 : " << std::hex << (unsigned int)crc_8() << std::endl; Sorry, I wasn't very clear in my last message.
The results can be unexpected, depending on whether the result of crc_8() (or more precisely, operator() ) is signed or unsigned. If operator() returns an unsigned value, there will be no problems, and the output will be ea.
On the other hand, if operator() returns a signed value, then it will first be promoted to a signed int, then converted to an unsigned int. Using the original example values posted by Victoria, the output would be ffffffea. In this scenario, the return value must be first cast to an unsigned char, then to an unsigned int: ... << static_cast<unsigned>( static_cast<unsigned char>( crc_8() ) )
I now understand what you meant. Yep, I was hit by this problem a couple of times.
I just had a quick look at the crc.hpp file, and operator() returns an unsigned value, thus banishing my caveat to the dusty corners where all academic footnotes end up ;-)
:-) I've implicitly though return type is always unsigned. Using signed types for bit-manipulation is way too messy. - Volodya
participants (2)
-
Jim.Hyslop
-
Vladimir Prus