How does one construct a mask for the MS bit?

BlandMA mab at druxp.UUCP
Tue Mar 12 09:11:07 AEST 1985


John Bass in article <169 at dmsd.UUCP> recommends:
>	#define MSB(a)		(1<<((sizeof a) * 8 - 1))

Stew Rubenstein in article <455 at harvard.ARPA> replies:
>	Is it portable to assume 8 bit bytes?  Answer:  no....

If you're running UNIX System V (and possibly other UNIX systems, I don't
know), take a look at /usr/include/values.h, which defines a number of
constants that might be of use, such as:

	BITSPERBYTE  number of bits in a byte
	HIBITS       bit mask for the high bit of a short
	HIBITI       ditto for int
	HIBITL       ditto for long
	MAXSHORT     maximum value of a signed short
	MAXINT       ditto for int
	MAXLONG      ditto for long

HIBITS, HIBITI, and HIBITL are macros defined in terms of BITSPERBYTE
using the same formula that John Bass gave.  BITSPERBYTE is conditionally
compiled using #if.

UNIX is a trademark of AT&T Bell Laboratories.
-- 
Alan Bland
{ihnp4, allegra}!druxp!mab
AT&T Information Systems Labs, Denver



More information about the Comp.lang.c mailing list