[comp.lang.c] Binary integer literals

george@hyper.lap.upenn.edu (George Zipperlen) (12/09/87)

Wanted: a way to declare binary constants!
Arrays of binaries intended as bitmaps are much easier to read and edit 
than arrays of hex declarations.  As an example here's a piece of C code:

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,  */
    0x0000,     /*  0000000000000000,  */
    0x0000,     /*  0000000000000000,  */
    0x0000,     /*  0000000000000000,  */
    0x0000,     /*  0000000000000000,  */
    0x0000,     /*  0000000000000000,  */
    0x0000 }    /*  0000000000000000 } */

I have a little filter that generates the whole line from the text within 
the comments, but it would be nice if the compiler (or the pre-processor)
could do this.  

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

If anyone reading this has the ear of ANSII, could they pass this suggestion on?

If it's too late for C, how about in C++ ?


Notes from some competing (-:) languages

FORTRAN 77 (UTX/32 from Gould)  B'00100101' c I've seen it in other dialects
DOMAIN Pascal (from Apollo)     2#10010     { I've seen it in other dialects }
Smalltalk-80                    2r1111110
Icon                            2R11011

OK, the first 2 are extensions to awkward languages, and the last 2 aren't 
system implementation languages (:-)

--------------------------------------------------------------------------------
George Zipperlen                    george@apollo.lap.upenn.edu
Language Analysis Project           george@hyper.lap.upenn.edu
University of Pennsylvania          Generic Disclaimer
Philadelphia, Pa.                   Cute saying
--------------------------------------------------------------------------------

amos@taux01.UUCP (Amos Shapir) (12/09/87)

In article <2752@super.upenn.edu> george@hyper.lap.upenn.edu (George Zipperlen) writes:
>Wanted: a way to declare binary constants!

Personally, I like hex/octal better; once you get used to it and have
the bit patterns in your head, it' much easier to use, e.g., in:
>    0x8200,     /*  1000001000000000,  */
quick, is bit 10 set or reset?
Now, try it again with 32-bit values...

-- 
	Amos Shapir			(My other cpu is a NS32532)
National Semiconductor (Israel)
6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel  Tel. +972 52 522261
amos%taux01@nsc.com (used to be amos%nsta@nsc.com) 34 48 E / 32 10 N

bright@dataio.Data-IO.COM (Walter Bright) (12/10/87)

In article <2752@super.upenn.edu> george@hyper.lap.upenn.edu (George Zipperlen) writes:
<Wanted: a way to declare binary constants!
<Arrays of binaries intended as bitmaps are much easier to read and edit 
<than arrays of hex declarations.  As an example here's a piece of C code:
<static unsigned short CursorPattern[16] = { 
<    0xff00,     /*  1111111100000000,  */
<    0x0000 }    /*  0000000000000000 } */
<What I would like to be able to do is to declare binary constants in some
<form like (just a suggestion):
<    0b#01000100 

This is implemented in Datalight C as 0b01000100. It is also in printf()
and scanf() as %b format. I sent a letter asking the ANSI C committee to
include it, but they cited a 'lack of prior art'. So I implemented it in
Datalight C so it is 'prior art' now. Pressure your C compiler vendor to
implement it also.

karl@haddock.ISC.COM (Karl Heuer) (12/10/87)

In article <2752@super.upenn.edu> george@hyper.lap.upenn.edu (George Zipperlen) writes:
>Wanted: a way to declare binary constants!

Yeah, I'd occasionally find that useful too.  The obvious syntax is 0b1011.

>[Can we get this to the ANSI committee?]

You'll get the usual response: "Need not convincingly shown; doable with
existing features; lack of prior art".  Since it's an easy feature to add, put
it in your local cc (or ask your vendor to do so) as an extension -- if it
becomes popular enough, it may make it into __STDC__==2.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

ado@elsie.UUCP (Arthur David Olson) (12/12/87)

> Wanted: a way to declare binary constants!
> Arrays of binaries intended as bitmaps are much easier to read and edit 
> than arrays of hex declarations. . .
> 
> static unsigned short CursorPattern[16] = { 
>     0xff00,     /*  1111111100000000,  */
>     0x8200,     /*  1000001000000000,  */
> ...

#include <stdio.h>

#define __	((((((((((((((((0
#define _	<<1|0)
#define X	<<1|1)

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

main()
{
	int	i;

	for (i = 0; i < sizeof CursorPattern / sizeof CursorPattern[0]; ++i)
		(void) printf("%04x\n", CursorPattern[i]);
	return 0;
}
-- 
ado@vax2.nlm.nih.gov		ADO, VAX, and NIH are Ampex and DEC trademarks