32 bit longs

Walter Bright bright at nazgul.UUCP
Sat Jan 19 18:39:32 AEST 1991


Many times I've seen code that says a type needs to be 32 bits wide when
what is really meant is >=32 bits is needed. This is typical when one
sees a typedef like:

typedef long int32;

This typedef implies that exactly 32 bits is required, rather than >=32.
Note that on a PDP-10, longs are 36 bits! A more portable approach would
be to have a typedef indicating what type you need, like:

typedef long fileoffset_type;	/* need at least 32 bits	*/

If you have a need for *exactly* 32 bits, you can do things like:
	x &= 0xFFFFFFFF;	/* truncate result to 32 bits	*/
Any decent compiler will not generate any code for that if x is
32 bits wide (and isn't volatile!). Alternatively, you can do things like:
	if (sizeof(x) == 4)
		/* code here depends on 32 bits in x */
		...
	else
		/* Portability alert! */
		assert(0); /* rewrite this algorithm! */

P.S. I prefer stuff like:
	#if sizeof(long) == 4
	...
	#else
	#error This algorithm needs to be ported
	#endif
but ANSI C seems to have torpedoed that.



More information about the Comp.lang.c mailing list