Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!linus!philabs!cmcl2!seismo!brl-tgr!tgr!cottrell@nbs-vms.arpa From: cottrell@nbs-vms.arpa (COTTRELL, JAMES) Newsgroups: net.lang.c Subject: Division With Shifts Message-ID: <1395@brl-tgr.ARPA> Date: Thu, 9-Jan-86 13:07:24 EST Article-I.D.: brl-tgr.1395 Posted: Thu Jan 9 13:07:24 1986 Date-Received: Sat, 11-Jan-86 05:44:49 EST Sender: news@brl-tgr.ARPA Lines: 30 /* > Just shifting using an arithmetic shift may give the wrong answer, but > you could correct the rounding and still get much faster execution like: > > shiftRightArithmetic register > branchIfPositive label > increment register > label: > > so -23/2 = -23>>1 + 1 = 11101001>>2 + 1 = 11110100 + 1 = 11110101 = -11 > which is the right answer... For -23, but not in the general case. Try an even number. The code must be like so (for the general shift case): Test Target BranchPositive label Add 2**Shiftcount - 1,Target label: ShiftRightArithmatic Target,Shiftcount So in order to divide by 2, add 1 & shift once; by 4 add three & shift twice; by 8 add 7 & shift thrice; etc > -Miles Miles from nowhere? jim cottrell@nbs */ ------