gcc interprets -0 incorrectly

Justin Pryzby justinpryzby@users.sourceforge.net
Fri Dec 6 15:12:00 GMT 2002


/*
 * GCC interprets -0 as -INT_MIN==-INT_MAX - 1 (See <limits.h>).
 * It knows how to overflow from positive to negative,
 * and from negative to positive.
 *
 * However, I object to this interpretation,
 * even if it does allow an int to hold an extra number;
 * From algebra: it is clear that for all x=-x, 2x=0, x=0.
 * Also, for all x=0, 2x=0, x=-x.
 * That is, x=-x implies that x=0, AND x=0 implies that x=-x.
 *
 * Please CC: me in all replies, as I am not subscribed to the list.
 *
 * Justin
 */


#include <stdio.h>
#include <assert.h>

int main()
{
	int i=0;
	struct byte_struct {
		int a:1;
		int b:6;
		int c:1;
	} *byte;

	byte=(struct byte_struct *) (((char *) &i)+3);
	byte->c=1;

	// -2147483648
	printf("%d\n", i);

	// warning: this decimal constant is unsigned only in ISO C90.
	i=-2147483648;
	i--;
	printf("%d\n", i);

	i=2147483647;
	i++;
	printf("%d\n", i);

	return 0;
}



More information about the Gcc mailing list