division

Thomas H{meenaho thomas at kuling.UUCP
Fri Jan 10 14:11:24 AEST 1986


In article <6263 at utzoo.UUCP> henry at utzoo.UUCP (Henry Spencer) 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.
>> 
>> The first is correct, the second is WRONG.  -1/2 is 0 not -1.
>

	[long story about different ways to divide negative numbers]

Enough about my comment on arithmetic shifts. I thought the problem was
the sign bit which is lost if one uses a logical shift.

			    x					       x
However the idea of adding 2  -1 when dividing a negative number with 2  seems
like a win.



-- 
Thomas Hameenaho, Dept. of Computer Science, Uppsala University, Sweden
Phone: +46 18 138650
UUCP: thomas at kuling.UUCP (...!{seismo,mcvax}!enea!kuling!thomas)



More information about the Comp.lang.c mailing list