Path: utzoo!attcan!uunet!munnari.oz.au!mudla!ok From: ok@mudla.cs.mu.OZ.AU (Richard O'Keefe) Newsgroups: comp.lang.c Subject: Re: binary constants (??) Keywords: macro, constant, binary Message-ID: <2765@munnari.oz.au> Date: 21 Nov 89 07:06:52 GMT References: <305@frf.omron.co.jp> <602@chem.ucsd.EDU> <7065@ficc.uu.net> Sender: news@cs.mu.oz.au Lines: 54 In article <7065@ficc.uu.net>, peter@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(_@___@@__@@___@_), bin(_@____________@_), bin(_@_@@@____@@@_@_), bin(_@__@______@__@_), bin(_@___@@@@@@___@_), 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.)