Two Birds with One Stone

John Woods, Software john at frog.UUCP
Wed Dec 25 02:23:41 AEST 1985

> In article <874 at dataioDataio.UUCP> bright at dataio.UUCP (Walter Bright writes:
> >Almost but not quite true. A compiler CANNOT normally replace a divide
> >by a right-shift if it is an integer divide. This is because a right
> >shift of a negative integer is not the same as a divide.
> However most useable processors provide arithmetic shifts which will give
> the right result even if it is a signed divide.
> -- 
> Thomas Hameenaho, Dept. of Computer Science, Uppsala University, Sweden

Wrongo:  Consider the following program:

main() {
	int three = -3;

	printf("%d %d\n", three / 2, three >> 1);

What does it print?

-1 -2

Division (usually) rounds negative numbers to zero,
ASH-ing right to -infinity.

John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc at MIT-XX.ARPA

The Pentagon's Polygraphs:  Witchcraft for witchhunts.

More information about the Comp.lang.c mailing list