type casting problem

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Sat Jun 11 14:27:13 AEST 1983


If you're willing to pick up a bunch of other cruft you can
#include <sys/param.h> (at least on x.yBSD) and get the constant
NBBY, number of bits per byte.  You also get NBPW, number of bytes
per word.  You unfortuantely get all sorts of uninteresting stuff
as well.

As for generating extract-macros, getting masks is easy:

#define	UNSIGNEDCHAR(c)		((c)&((1<<NBBY)-1))

The shift factor is just NBBY:

#define	MIDDLEBYTE(w)		UNSIGNEDCHAR((w)>>NBBY)

How about an include file called <machdep.h>, containing lots of
things like the above, and/or the version of C being run (say
perhaps #define MANY_UNSIGNEDS means the compiler understands
unsigned long and unsigned char)?  Possibly these might just be
macros, or even typedefs as in <sys/types.h>:

	<machdep.h> on a Vax running 4BSD
	typedef unsigned char u_char;
	#define U_CHAR(c)	(c)

	<machdep.h> on an 11/45 running V7
	typedef char u_char;
	#define U_CHAR(c)	((c)&0377)

and so on.  At least the ``fundamental constants'' (NBBY, NBPW)
should be available someplace other than <sys/param.h>.

			- Chris ({allegra,seismo}!umcp-cs!chris)



More information about the Comp.lang.c mailing list