Division With Shifts

Chris Torek chris at umcp-cs.UUCP
Fri Jan 10 14:01:42 AEST 1986


I think everyone agrees that for nonnegative integers,
(i >> 1) == (i / 2).  So let us ignore those.  For negative
integers, (i >> 1) tends to round towards -infinity instead
of towards zero.  To fix this all you need to do is add one
before shifting.

I was going to show why, but it hardly seems worth the effort.
It just works.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list