Determining machine characteristics (was: IsUnsigned() function?)

Karl Heuer karl at haddock.ima.isc.com
Wed Jul 25 11:18:23 AEST 1990


In article <1990Jul22.154326.19680 at ux1.cso.uiuc.edu> mcdonald at aries.scs.uiuc.edu (Doug McDonald) writes:
>[Assuming the question was to analyze the *type*, can it be done portably?]

Already solved, see my earlier post.

>To extend it a bit, how about macros for [the format of negative numbers]

Assuming there are only the three possibilities,
	#define is_twos_complement() ((-2 | -3) == -1)
	#define is_ones_complement() ((-1 & -2) == -3)
	#define is_sign_magnitude()  ((-1 | -2) == -3)
should distinguish them.

>word_size(a), which returns the number of bits in a

I suppose the simple solution
	#include <limits.h>
	#define word_size(a) (sizeof(a)*CHAR_BIT)
is cheating.  Hmm, if you want it to be *completely* portable, allowing for
arbitrarily large values of CHAR_BIT (64 might actually be a reasonable option
on a Cray!), I can't think of a way to do it offhand.

I'd also like to be able to do `is_big_endian()' and `is_little_endian()', but
I think these require run-time code.

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list