[net.lang.c] How does one construct a mask for th

stevel@haddock.UUCP (03/06/85)

>> /*
>> This would require generation of a mask for the most significant bit
>> */

#define MSB (~(-1>>1))

Well know we know /* */'s name is Jim, anybody know what machine he is
on. If only he would learn how to program and/or sign his path.

Steve Ludlum, decvax!yale-co!ima!stevel, {cca!ihnp4!cbosgd}!ima!stevel

ron@brl-tgr.ARPA (Ron Natalie <ron>) (03/06/85)

> >> /*
> >> This would require generation of a mask for the most significant bit
> >> */
> 
> #define MSB (~(-1>>1))
> 
Surprise, this doesn't even work on VAX's and PDP -11's.  -1>>1 is still
-1.  Be careful when coming up with ideas like these.  C has also been
implemented on ones-complement (and subtractive arithmatic) machines.
Right shifting of signed quantities is defined to be machine depenedant
in C.

-Ron

ed@ISM780.UUCP (03/07/85)

>    #define MSB (~(-1>>1))
>
>    Steve Ludlum, decvax!yale-co!ima!stevel, {cca!ihnp4!cbosgd}!ima!stevel

I think you need

#define MSB (~((unsigned)-1>>1))

Right shift is only guaranteed to fill by zeroes if the left operand is
unsigned.

Ed Lycklama
decvax!cca!ima!ism780!ed

stevel@haddock.UUCP (03/09/85)

> > #define MSB (~(-1>>1))
>
> Surprise, this doesn't even work on VAX's and PDP -11's.  -1>>1
> is still -1.  Be careful when coming up with ideas like these.
> C has also been implemented on ones-complement (and subtractive
> arithmatic) machines.  Right shifting of signed quantities is
> defined to be machine depenedant in C.

Next time I'll try it on the vax compiler instead of my own pcc based
one. How about this.

msb(){ register unsigned a; return( ~((a= -1)>>1) ); }

or for the truer minimalists

msb(){register unsigned a;return~((a= -1)>>1);}

or for the one who like to live dangerously

static unsigned _foo_bar
#define MSB (~((_foo_bar= -1) >> 1))

I know what you are saying Jim. What if I get a signal while
prosessing the macro and invoke it again. Well the word on the
vax is that you are in luck it only uses the in register value.

Steve Ludlum, decvax!yale-co!ima!stevel, {ihnp4!cbosgd}!ima!stevel

PS these macros/functions are certified to work on V.2.2 vax C compiler
for whatever thats worth.