Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!agate!labrea!rutgers!bellcore!u1100a!krohn From: krohn@u1100a.UUCP (Eric Krohn) Newsgroups: comp.lang.c Subject: Re: Efficient coding considered harmful? Message-ID: <2131@u1100a.UUCP> Date: 8 Nov 88 20:06:47 GMT References: <3105@hubcap.UUCP> <34112@XAIT.XEROX.COM> <1700@dataio.Data-IO.COM> <8630@smoke.ARPA> <1704@dataio.Data-IO.COM> <119@twwells.uucp> <7700@bloom-beacon.MIT.EDU> <137@twwells.uucp> <8819@smoke.BRL.MIL> <143@twwells.uucp> Reply-To: krohn@u1100a.UUCP (Eric Krohn) Organization: Bell Communications Research, Piscataway, NJ Lines: 28 In article <143@twwells.uucp> bill@twwells.UUCP (T. William Wells) writes: ] ] Formulated as a general principle: if an operand in a multiply or the ] divisor in a divide or mod (with the other operand being known to be a ] positive number) is one which in the problem being solved is ] naturally a power of two, it is reasonable to use bitwise operators to ] implement the operation, provided that a #define is used to define ] the number and (when needed) its log base 2. Instead of #defining the log in terms of the number, I prefer to #define the number in terms of its log base 2, as in #define LOGSIZE 7 #define SIZE (1 << (LOGSIZE)) to insure that SIZE really is a power of 2 when I expect it to be. For the safety conscious, you can add #if LOGSIZE <= 0 #include "LOGSIZE must be non-negative" #endif #if LOGSIZE >= BITSPERWORD #include "LOGSIZE is too large" #endif -- -- Eric J. Krohn krohn@ctt.ctt.bellcore.com or {bcr,bellcore}!u1100a!krohn Bell Communications Research, 444 Hoes Ln, Piscataway, NJ 08854