4.3.5 Negative Numbers

Negative numbers are not the same on 32-bit versus 64-bit processors. Negative numbers are type INTN. Type INTN is a 4-byte container on a 32-bit processor and an 8byte container on a 64-bit processor. For example, -1 on a 32-bit CPU is 0xFFFFFFFF, and -1 on the 64-bit CPU is 0xFFFFFFFFFFFFFFFF.

Caution: Be careful when assigning or comparing negative numbers. Negative numbers have different values on different architectures. For example, do not compare -1 to 0xFFFFFFFF, compare -1 to -1 and compare 0xFFFFFFFF to 0xFFFFFFFF. If the size of the values change, then the same compares may return different results.

The following example shows sample code that compiles without errors or warnings on both 32-bit and 64-bit architectures. However, the sample behaves very differently on 32-bit architectures versus 64-bit architectures.

Example 11-Negative number example

UINT32 ValueU32;
ValueU32 = 0xFFFFFFFF;
if ((INTN)ValueU32 == -1) {
//
// This message is printed on 32-bit CPUs.
// This message is not printed on 64-bit CPUs.
//
Print (L"Equal\n");
} else {
//
// This message is not printed on 32-bit CPUs.
// This message is printed on 64-bit CPUs.
//
Print (L"Not Equal\n");
}