shift left arithmetic

Andries Brouwer aeb at cwi.nl
Mon Sep 10 23:15:26 AEST 1990


While debugging an Australian computer algebra package
I stumbled over the following: 1<<32 produced 0, but
int a; a=1; a<<32 produced 1. (Of course the word length
of my machine is 32 bits.)
Now I can understand how these values come about, and
each is perfectly acceptable, but shouldnt static evaluation
of expressions produce the same answers as dynamic evaluation?
Is my compiler broken?

[In the application the programmer wrote code depending on
#if 1<<BITS_PER_WORD
; this adds the question whether also the preprocessor should
be consistent in these matters.]

---
main(){
	int a = 1;
	unsigned int b = 1;

	printf("According to the preprocessor, 1<<32 is %s\n",
#if 1<<32
		"nonzero"
#else
		"zero"
#endif
	);

	printf("at compile time it is %d; at run time we get %d %d\n",
		1<<32, a<<32, b<<32);
}
---
According to the preprocessor, 1<<32 is zero
at compile time it is 0; at run time we get 1 1



More information about the Comp.std.c mailing list