INF: Bitwise Complement Operator Appears to Fail on Comparison

ID Number: Q31510

4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

The bitwise complement operator (~) may appear to work incorrectly

when used to compare unsigned characters, as illustrated in the

following example.

However, when using the bitwise complement operator, it is

important to note that it will perform the "usual arithmetic

conversions" on operands. The usual arithmetic conversions are

described in detail in Section 5.3.1 of "The Microsoft C Language

Reference Guide."

More Information:

The following program prints out as "failed" even though it appears

that the two items should compare as "equal":

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

main()

{

unsigned char i,j;

unsigned char k = 4;

i = k;

j = ~i;

if (j == ~i)

printf("passed\n");

else

printf("failed\n");

}

The compiler takes the steps below to evaluate the following

statement:

if (j == ~i)

1. The compiler converts the operand "i" to an unsigned integer (in

the C manual, see step 5 of the usual arithmetic conversions).

2. The compiler complements the bits of this unsigned integer (the

high byte becomes 0xFF).

3. The compiler converts the operand "j" to an unsigned integer (the

high byte becomes 0x00).

4. The compiler compares the two operands.

Because the high bytes of the two operands differ, the comparison

will fail.

To ensure that the compiler will compare only the low bytes of the two

operands, cast the operand that is being complemented. For example,

you can change the comparison to the following:

if (j == ~(unsigned char)i)

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00 7.0 6.0