August 29, 2011

Experiences with Signedness

Signed comparasion involves the following instructions: JG, JGE, JL, JLE, JNG, JNGE, JNL, JNLE. Unsigned comparasion: JA, JAE, JB, JBE, JNA, JNAE, JNB, JNBE.

Let's assume value variable has a type of signed integer, that is compared to particular const values. The following will perform signed comparison.
if (value < 0x7fffffff) {}
The following will perform unsigned comparison.
if (value < 0x80000000) {}
Inference: 0x7fffffff is stored as signed integer but 0x80000000 is stored as unsigned integer.

Let's take the above example except value variable has a type of signed char, rather than signed integer, that is compared to particular const values. The results regarding the comparison will be the same as above.
Inference: the comparison will be performed according to the signedness of larger type.

Let's assume value1 has a type of signed integer, and value2 has a type of unsigned integer. The values are compared and the comparison will be unsigned as you have guessed.
if (value1 < value2) {}

Let's assume value1 has a type of unsigned char, and value2 has a type of signed integer. The values are compared and the comparison will be signed as you have guessed.
if (value1 < value2) {}

Important, that the above based on observation, on Windows OS, on IA-32 architecture. When you develop a program it's better to rely on the definition of the language rather than solely on observation.
  This blog is written and maintained by Attila Suszter. Read in Feed Reader.