Path: utzoo!attcan!uunet!ncrlnk!ncrcae!ece-csc!ncsuvx!mcnc!ecsvax!dukeac!sbigham From: sbigham@dukeac.UUCP (Scott Bigham) Newsgroups: comp.lang.c Subject: /, >>, %, and other ugliness... Message-ID: <1081@dukeac.UUCP> Date: 4 Nov 88 22:25:51 GMT References: <3105@hubcap.UUCP> <34112@XAIT.XEROX.COM> <1700@dataio.Data-IO.COM> <136@twwells.uucp> <2730@hound.UUCP> <11529@bellcore.bellcore.com> Reply-To: sbigham@dukeac.UUCP (Scott Bigham) Organization: Academic Computing, Duke University, Durham, NC Lines: 94 In article <11529@bellcore.bellcore.com> sjs@ctt.bellcore.com (Stan Switzer) writes: >Well, I really hate to open this can of worms yet again, but in my >experience whenever (for integer i) "i%4" and "i>>2" differ... Errr... in my experience, i%4 and i>>2 will almost always differ. They're not even doing the same thing. >, what you >REALLY want is what "i>>2" does anyway (assuming 2's complement). I'll assume (possibly incorrectly) that you meant i/4 when you said i%4 and answer below. >Which is to say that you want the MODULUS operation rather than the >REMAINDER operation. Errr... The modulus operation IS the remainder operation. > I have seen MANY cases where this is true, and >have NEVER seen a case where it was false. (See my assumption above) Consider: double r; int i=5; . . . r=i/4; What I want is not what i>>2 does, nor, for that matter, what i/4 does. What I want is what i/4 -should- do; ie. return r=1.25. Yes, I know I can say r=(double)(i/4). The irony here is that C can't make this simple conversion, and yet will gladly convert a char * into a FILE * when I don't even want it to. >The PC/RT implements ... i%n to always >return a value (0..n-1) for positive "n" (dunno about negative "n"). Then you're lucky. All the C's I've ever used thought (-1)%5==-1. >Please, do not argue that integer -1/2 should be -1 so that the sign >is the same as -1.0/2.0 or because "by the laws of arithmetic" (-1)/2 >equals -(1/2). These are pointless arguments that have no useful >consequences as far as correct programs are concerned. By definition, the greatest integer of x is the largest integer n such that n<=x; then integer -1/2 is -1. Do programs that use correct math not count as correct programs? >Anyone wishing to take up the other side of the argument must find an >example of an actual situation where having -1/2 yield -1 is useful. Actually, I'd MUCH rather have -1/2 yield -0.5... >1) [0..3] represents [north, east, south, and west]. With the modular >% operator, "dir = (dir-1)%4" means "turn left". I first came across >this in a program that drew Hilbert curves. Be careful; as I mentioned above, some C's don't get this right. This has bitten me several times. >2) Bit extraction: To get the n'th bit from the current (char) pointer >"p" (0 bit is low) use "bit = (p[i/BITSPERCHAR]>>(i%BITSPERCHAR)) & 1" >This comes up in rasterization code often enough. The usual solution >is to jimmy it so that you avoid negative "i" (or just use >>3 and &7 >instead). Yes, if you're doing bit manipulation, then bitwise operations are the natural choice. If you're doing arithmetic, than arithmetic operations are the natural choice. >You should never have to code "i>>3" when you mean "i/8". Exactly. At least three people have said this already. If you mean i/8, -say- i/8. > You should >also never have to worry that a 45 cycle divide instruction is going >to be used so that in case the quotient is negative you'll get the >answer that someone thought you wanted instead of the one you probably >really want anyway... Errr... when I say i/-4, I want i/-4. You seem to think I want something different. >Stan Switzer sjs@ctt.bellcore.com sbigham -- Scott Bigham "The opinions expressed above represent Internet sbigham@dukeac.ac.duke.edu me and everyone that agrees with me. USENET sbigham@dukeac.UUCP If that includes Duke University, ...!mcnc!ecsgate!dukeac!sbigham I'll be amazed."