Is something wrong with the compiler ?

Tim Bomgardner bomgard at iuvax.cs.indiana.edu
Thu Sep 27 02:17:15 AEST 1990


In article <884 at gtenmc.UUCP> csp at 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 at 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.



More information about the Comp.lang.c mailing list