Path: utzoo!attcan!uunet!snorkelwacker!usc!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cica!iuvax!bomgard From: bomgard@iuvax.cs.indiana.edu (Tim Bomgardner) Newsgroups: comp.lang.c Subject: Re: Is something wrong with the compiler ? Message-ID: <60399@iuvax.cs.indiana.edu> Date: 26 Sep 90 16:17:15 GMT References: <884@gtenmc.UUCP> Organization: Indiana University, Bloomington Lines: 32 In article <884@gtenmc.UUCP> csp@gtenmc.UUCP (Charudutta S Palkar) writes: } } Assuming that the 2's complement system is used to represent the negative } integers. I wrote the following code , and the results I got were absurd. } }C code : } } main() } { } int a; } printf(" Maxint : %d\na = %d\n", ( int )(( unsigned ) ~0 >> 1 ) , } a = ( int )(( unsigned ) ( a = ~0 ) >> 1 )); } } } } Output : } } Maxint : -1 } a = 2147483647 } }Can some complier writer tell me why this is happening ? } }csp - csp@gtenmc.UUCP } }K&R C > ANSI C The value for a is exactly what I would expect: pow(2,31)-1. The question is is ~0 in the expression ((unsigned) ~0 >> 1) signed or unsigned. If it is signed, then the result is implementation-defined and can be either a logical or an arithmetic shift. -1 is the correct result for an arithmetic shift. However, you have a valid cast, so ~0 is unsigned and a logical shift should be performed. I believe your compiler is in error.