Clarify a line of C code please

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I get a warning - which I'f like to remove on the following line of code.
It's part of a crc generator but not written by me and the warning is 'operation on 'ans' may be undefined. Any clues anyone?
Thanks

The code snippet in question is:


#define POLYNOMIAL 0x8005


if (ans & 0x08000)
{
ans = (ans <<= 1) ^ POLYNOMIAL; // Warning HERE!
}
else
{
ans << = 1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
(ans <<= 1)
What are you trying to do here? It seems a strange statement but someone else may shed some light.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If we look at numerous crc examples on the net, we often see something like:

      crc <<= 1;
      crc ^= polynomial;

What must have happened is that somebody "smart" decided to optimize that and combined the two statements into a single one. But did not care to remove the first assignment. It must have been a different compiler that did not complain. gcc obviously does not like two assigments to the same variable in a single statement. I would guess what you want is this:

ans = (ans << 1) ^ POLYNOMIAL;

Eugene