[comp.lang.c] Bitmap constants in C code

kdmoen@watcgl.waterloo.edu (Doug Moen) (12/12/87)

george@hyper.lap.upenn.edu (George Zipperlen) writes:
>static unsigned short CursorPattern[16] = { 
>    0xff00,     /*  1111111100000000,  */
>    0x8200,     /*  1000001000000000,  */
>    0x8400,     /*  1000010000000000,  */
>    0x8200,     /*  1000001000000000,  */
>    0x8100,     /*  1000000100000000,  */
>    0xa080,     /*  1010000010000000,  */
>    0xd040,     /*  1101000001000000,  */
>    0x8880,     /*  1000100010000000,  */
>    0x0500,     /*  0000010100000000,  */
>    0x0200,     /*  0000001000000000,  */

>What I would like to be able to do is to declare binary constants in some
>form like (just a suggestion):
>    0b#01000100 

George wants to type in the bitmap pattern directly, without being forced
to translate it into octal or hexadecimal.

Here is a way to do this in ANSI C (no language extensions necessary):

#define A +128
#define B +64
#define C +32
#define D +16
#define E +8
#define F +4
#define G +2
#define H +1
#define _

static unsigned char CursorPattern[32] = {
	A B C D E F G H,_ _ _ _ _ _ _ _,
	A _ _ _ _ _ G _,_ _ _ _ _ _ _ _,
	A _ _ _ _ F _ _,_ _ _ _ _ _ _ _,
	A _ _ _ _ _ G _,_ _ _ _ _ _ _ _,
	A _ _ _ _ _ _ H,_ _ _ _ _ _ _ _,
	A _ C _ _ _ _ _,A _ _ _ _ _ _ _,
	A B _ D _ _ _ _,_ B _ _ _ _ _ _,
	A _ _ _ E _ _ _,A _ _ _ _ _ _ _,
	_ _ _ _ _ F _ H,_ _ _ _ _ _ _ _,
	_ _ _ _ _ _ G _,_ _ _ _ _ _ _ _,
	...

Personally, I find this easier to read than binary integer literals,
although its slightly more painful to type in.

Note that the macros given above make use of unary +.
It should not be difficult to come up with a similar set of
macros that work in "real" (pre-ansi) C.
-- 
Doug Moen
University of Waterloo Computer Graphics Lab
UUCP:     {ihnp4,watmath}!watcgl!kdmoen
INTERNET: kdmoen@cgl.waterloo.edu

garys@bunker.UUCP (Gary M. Samuelson) (12/24/87)

In (belated) response to George Zipperlan and Doug Moen:

George wanted to enter binary constants (e.g., 0b#01000100).
Doug suggested some tricky defines:

	#define A +128
	#define B +64
	#define C +32
	...

	char pattern[] = { A _ C _ E _ F _, ... };

This allows binary constants in ANSI C.  I applaud Doug's
ingenuity, and suggest the following alternative:

#define X )*2+1
#define _ )*2
#define b ((((((((0		/* For byte building */
#define w ((((((((((((((((0	/* For word building */

George's original problem (bit pattern for an arrow) is thus:

static unsigned short CursorPattern[] =
{
	w X X X X X X X X _ _ _ _ _ _ _ _,
	w X _ _ _ _ _ X _ _ _ _ _ _ _ _ _,
	w X _ _ _ _ X _ _ _ _ _ _ _ _ _ _,
	w X _ _ _ _ _ X _ _ _ _ _ _ _ _ _,
	w X _ _ _ _ _ _ X _ _ _ _ _ _ _ _,
	w X _ X _ _ _ _ _ X _ _ _ _ _ _ _,
	w X X _ X _ _ _ _ _ X _ _ _ _ _ _,
	w X _ _ _ X _ _ _ X _ _ _ _ _ _ _,
	w _ _ _ _ _ X _ X _ _ _ _ _ _ _ _,
	w _ _ _ _ _ _ X _ _ _ _ _ _ _ _ _
};

There are three improvements over Doug's solution:
1. Unary '+' is not required.
2. You don't have to remember which letter goes in which column.
3. It is just as easy to build any size of binary datum.

Gary Samuelson

Should I have saved those macros for the next obfuscated C contest?