Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reworked first NCE/CS. Will add more CS's.

...

Noncompliant Code Example

The In this noncompliant code example attempts to take two's complement of a value of type unsigned long using a mask, on implementations where sizeof(unsigned long) == sizeof(unsigned int), each bit of x is correctly flipped using mask.

However, on implementations where sizeof(unsigned long) > sizeof(unsigned int), mask will have leading zeros, e.g. mask == 0x00000000FFFFFFFF. Then, only the lower-order bits of x will be flipped.

Code Block
bgColor#FFCCCC

// (Incorrect) Set all bits in mask to one.
const unsigned long mask = 0xFFFFFFFF;  

unsigned long x;

/* Initializeflipbits(unsigned long x) */{

x  =return (x ^ mask) + 1;

This code produces the two's complement of x for implementations where unsigned long have a precision of 32 bits. However, on implementations where unsigned long is larger than unsigned int, this code has incorrect behavior. If unsigned long has a precision of 64 bits, and unsigned int has a precision of 32 bits, only the lower order 32-bits are set, and the resulting value of mask is 0x00000000FFFFFFFF.

Compliant Solution

}

Compliant Solution (-1)

In this compliant solution, the integer constant -1 cannot be represented by a variable of type unsigned long. According to the C99 standard, Section 6.3.1.3, Paragraph 2,

Wiki Markup
\[If the value can't be represented by the new type and\] if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

"One more than the maximum value that can be represented in the new type," ULONG_MAX + 1, is added to -1, resulting in a right-hand-side value of ULONG_MAX. The representation of ULONG_MAX is guaranteed to have all bits set to one by Section 6.2.6.1, Paragraph 3,

Values stored in unsigned bit-fields and objects of type unsigned char shall be represented using a pure binary notation.

(emphasis added)

and by Section 6.2.6.2, Paragraph 1,

For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N ? 1 , so that objects of that type shall be capable of representing values from 0 to 2N ? 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.

(emphasis added)

By the same reasoning, -1 is suitable for setting all bits to one of any unsigned integer variableA portable (and safer) way to set all the bits on in an integer constant of type unsigned long is to define a constant with a value of a complement of 0 (defined to be the appropriate type).

Code Block
bgColor#CCCCFF

// (Correct) Set all bits in mask to one.
const unsigned long mask = ~0UL-1;

unsigned long x;

/* Initializeflipbits(unsigned long x) */{

x  =return (x ^ mask) + 1;

...

;
}

Noncompliant Code Example

...

This code has the desired effect for implementations where unsigned long have a precision of 32 bits but not on implementations where unsigned long has a precision of 64 bits.

Compliant Solution

A portable (and safer) way of setting the high order bit is to use a shift expression as in this compliant solution.

...