Efficient coding considered harmful?

Eric Krohn krohn at u1100a.UUCP
Wed Nov 9 06:06:47 AEST 1988


In article <143 at twwells.uucp> bill at 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 at ctt.ctt.bellcore.com  or  {bcr,bellcore}!u1100a!krohn
Bell Communications Research,	444 Hoes Ln,    Piscataway, NJ 08854



More information about the Comp.lang.c mailing list