Use scalable masks with scalable typedefs.
Scalable types, such as int
and long, may
be different sizes on different platforms. For example, use the
portable bit mask construct:
Solution 1:
#include <inttypes.h> #ifdef __LP64__ int64_t node_id=0xfffffffffffffffc; #else int32_t node_id=0xfffffffc;
|
or:
Solution 2:
#include <inttypes.h> intmax_t node_id = ~0x3;
|
instead of the non-portable construct:
long node_id = 0xfffffffc;
|
When compiled on an HP-UX 32-bit platform, the first 2 constructs
above result in the intended value:
1111 1111 1111 1111 1111 1111 1111 1100
|
However, when compiled on an HP-UX 64-bit platform, the first
and second construct produce the correct binary value:
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100
|
while the third construct generates an incorrect bit mask
value:
0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1100
|
Use fixed size masks with fixed size type definitions. For
example, if you want a 32-bit mask, use the portable construct:
#include <inttypes.h> int32_t intmask = INT32_MAX;
|
instead of:
long intmask = 0x7fffffff; |