binary constants (??)

Richard O'Keefe ok at mudla.cs.mu.OZ.AU
Tue Nov 21 18:06:52 AEST 1989


In article <7065 at ficc.uu.net>, peter at ficc.uu.net (Peter da Silva) writes:
> For most cases binary constants are just fine in HEX. But for image data
> it's a real pain.

Who said everything the C compiler sees had to be written by hand?
If you need binary numbers, use m4.  It's a UNIX utility, and there is
a public domain version by Ozan Yigit.

Here's all you need for binary numbers, and you don't really
need rhead and rtail as separate macros:

define(rhead,`substr($1,decr(len($1)),1)')dnl
define(rtail,`substr($1,0,decr(len($1)))')dnl
define(bin,`ifelse($1,,0,`eval(bin(rtail($1))*2+rhead($1))')')dnl

With the aid of this, Peter da Silva would just write
    struct sprite smileyface =
	{
	    0, 0, 32, 011,
	    {
		bin(0000000000000000),
		bin(0000111111110000),
		bin(0011000000001100),
		bin(0100011001100010),
		bin(0100000000000010),
		bin(0101110000111010),
		bin(0100100000010010),
		bin(0100011111100010),
		bin(0011000000001100),
		bin(0000111111110000),
		bin(0000000000000000),
	    }
	};

If we change the definition of bin to
define(bin,`ifelse($1,,0,`eval(bin(rtail($1))*2+
	    ifelse(rhead($1),0,0,rhead($1),_,0,1))')')dnl

the bit map could be either as shown above OR as shown below

		bin(________________),
		bin(____@@@@@@@@____),
		bin(__@@________@@__),
		bin(_ at ___@@__@@___ at _),
		bin(_ at ____________@_),
		bin(_ at _@@@____@@@_ at _),
		bin(_ at __@______ at __@_),
		bin(_ at ___@@@@@@___ at _),
		bin(__@@________@@__),
		bin(____@@@@@@@@____),
		bin(________________),

I find this version clearer still.  (By the way, it took 20 minutes to
knock this together, I'm rather rusty with M4.)



More information about the Comp.lang.c mailing list