How does one construct a mask for the MS bit?

Ken Turkowski ken at turtlevax.UUCP
Thu Feb 28 07:42:59 AEST 1985


The VAX and PDP-11 have the property that
	int x;
	x = 12000;
	write(df, &x, 2);
gives the same results whether an int is short or long.
On the 68000, this gives different results.

This could be interpreted as follows:  truncation of bytes or words for
the DEC computers are appropriate for integers, whereas truncation for
the 68000 is appropriate for fractions.  Taking the "first" digit of
12345 yields 5 for DEC, and 1 for Motorola.

I would like to be able to have the write sequence above give the same
results for the 68000, regardless of whether the int is long or short.

This would require the generation of a mask for the most significant
bit of an int, where the int could be any arbitrary size.  Does the
following not work on any machine?

main() {
	int x;
	x = (-1) & ~((unsigned)(-1) >> 1);
	printf("%x\n", x);
}

Then I could sequence through bits as follows:

for (i = 0; i < N; i++)
	if (joe & (ms >> i))
		printf("Bit %d of joe is set\n", i);

-- 

Ken Turkowski @ CADLINC, Menlo Park, CA
UUCP: {amd,decwrl,nsc,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken at DECWRL.ARPA



More information about the Comp.lang.c mailing list