Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This compliant solution eliminates the possibility of undefined behavior resulting from a left shift operation on signed and integers, it also uses unsigned integers in accordance with INT13-A. Use bitwise operators only on unsigned operands. Smaller sized integers are promoted according to the integer promotion rules (see INT02-A. Understand integer conversion rules).

Code Block
bgColor#ccccff
unsigned int si1, si2, sresult;

if ( (si1 < 0) 
  || (si2 < 0) 
  || (si2 >= sizeof(int)*CHAR_BIT) 
  || (si1 > (INT_MAX >> si2)) ) 
{
  /* handle error condition */
}
else {
  sresult = si1 << si2;
}

...