Next: Arrays and Pointers Up: Bit operations Previous: Bit operations

Some remarks on portability

(i) Care is needed if portable programs contain bit operations (in particular). As well as the features already noted, you can adopt a style which is inherently portable:

   x & ~077

is portable, because it is independent of wordlength.

   x & 0177700

gives apparently the same effect, but only if the machine has a 16 bit word. (How many leading ones?)

(ii) It is a good idea to avoid having these bit constants littered through your program. Use defines, such as:

#define BYTE_MASK 0xFF

(this is strictly not part of C, but is a C pre-processor operation. It must appear at the left-hand edge of a new line)

(iii) Be very careful to distinguish and ; | and ||. Often either is valid C so the compiler will not spot help you: it is quite hard to find this bug when re-reading a program. You could try:

#define BIT_AND &
#define LOGICAL_AND &&

etc.



Next: Arrays and Pointers Up: Bit operations Previous: Bit operations


maspjw@
Tue Sep 27 15:29:34 BST 1994