Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX 64-bit Porting and Transition Guide: HP 9000 Computers > Chapter 5 Writing Portable Code

Using Portable Bit Masks

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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;
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1998 Hewlett-Packard Development Company, L.P.