Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site dataioDataio.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!vax135!cornell!uw-beaver!uw-june!entropy!dataio!bright From: bright@dataioDataio.UUCP (Walter Bright) Newsgroups: net.lang.c Subject: Re: Two Birds with One Stone Message-ID: <874@dataioDataio.UUCP> Date: Tue, 10-Dec-85 13:02:09 EST Article-I.D.: dataioDa.874 Posted: Tue Dec 10 13:02:09 1985 Date-Received: Thu, 12-Dec-85 04:35:01 EST Reply-To: bright@dataio.UUCP (Walter Bright Organization: Data I/O Corp., Redmond WA Lines: 25 In article <119@hadron.UUCP> jsdy@hadron.UUCP (Joseph S. D. Yao) writes: >I believe that this falls under the heading, "say what you mean." >If you m e a n that bits should shift left, say so; if you instead >m e a n that you wish to multiply (resp., divide); say that, also! >Your compiler (at least, with optimisation on) should be able to do >the conversion if it speeds anything up. 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. { int i; unsigned u; i *= 4; ==> i <<= 2; u *= 4; ==> u <<= 2; i /= 4; ==> i >>= 2; WRONG! u /= 4; ==> u >>= 2; } Since divides are relatively expensive, one would want them replaced by shifts whenever possible. The only way you can get the compiler to do this is by casting the int to unsigned before the divide is done (but check the output of the compiler to make sure it is doing things correctly).