Versions Compared

Key

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

...

Wiki Markup
This noncompliant code example can result in an error condition on [implementations|BB. Definitions#implementation] in which an arithmetic shift is performed, and the sign bit is propagated as the number is shifted \[[Dowd 2006|AA. Bibliography#Dowd 06]\].

Code Block
bgColor#FFcccc
langc
int rc = 0;
int stringify = 0x80000000;
char buf[sizeof("256")];
rc = snprintf(buf, sizeof(buf), "%u", stringify >> 24);
if (rc == -1 || rc >= sizeof(buf)) {
  /* handle error */
}

...

In this compliant solution, stringify is declared as an unsigned integer. The value of the result of the right shift operation is the integral part of the quotient of stringify / 2^24.

Code Block
bgColorccccff
langc
int rc = 0;
unsigned int stringify = 0x80000000;
char buf[sizeof("256")];
rc = snprintf(buf, sizeof(buf), "%u", stringify >> 24);
if (rc == -1 || rc >= sizeof(buf)) {
  /* handle error */
}

...

INT13-EX1: When used as bit flags, it is acceptable to use preprocessor macros as arguments to the & and | operators even if the value is not explicitly declared as unsigned.

Code Block
bgColor#ccccff
langc
fd = open(file_name, UO_WRONLY | UO_CREAT | UO_EXCL | UO_TRUNC, 0600);

INT13-EX2: If the right hand side operand to a shift operator is known at compile time, it is acceptable for the value to be represented with a signed type provided it is positive.

Code Block
bgColor#ccccff
langc
#define SHIFT 24
foo = 15u >> SHIFT;

...