Topic: Binary Data and C/C++
Author: comeau@panix.com (Greg Comeau)
Date: 1997/01/15 Raw View
In article <5beerd$k0u@erawan.cognex.com> Michael R Cook
<mcook@cognex.com> writes:
>>>>>> "CG" == Chelly Green <chelly@eden.com> writes:
>
> CG> Maybe because hex is sufficient, or entering binary numbers is very
> CG> error-prone.
>
>It would be less error-prone if C/C++ were to allow underscores within
>numeric literals (as some other languages do).
This too (and other things) was discussed w.r.t the binary number
proposal. I see to recall it somewhat like Fergus did: that although
it wasn't all that unreasonable to consider this, that it's impact was
not clear and that we should take C's lead, and so, it never made
it further than the working group.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,342
1
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: feb6399@osfmail.isc.rit.edu (Frank Barrus)
Date: 1997/01/15 Raw View
In article <5b8g4t$ri3@granny.mac.co.nz>, <markj@netaccess.co.nz> wrote:
>Hi,
>I've got a question which I have always wondered about regarding
>the C and C++ standards.
>
>Why does C/C++ lack a binary number qualifier. For example I would like
I've always wondered the same thing myself.
>to do this:
>
>unsigned char u = 01100011b;
>
Actually, to follow the standard pattern, I think it should
be:
unsigned char u = 0b01100011;
(since octal is prefixed with a '0', and hex with a '0x', it makes
sense to implement the binary qualifier as a prefix)
This is the way I've always implemented it in my own C and C++
parsers, and my own versions of strtol(), strtoul(), etc...
>but instead I always have to use the hex version of 0x63 or invent some
>horrible binary macro.
>
>If there is no good reason why I should not be able to this then I would
>appreciate
>it if somebody in the know could propose it to the C/C++ standards committee.
>
>It would certainly make my programming life much easier.
I, too, would be interested in hearing the reasoning for not having
this, as there have been many occasions on which I would have found
it useful.
- Frank
--
Frank "Shaggy" Barrus: shaggy@csh.rit.edu; http://www.csh.rit.edu/~shaggy
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Abhishek Chauhan <abhishek.chauhan@sun.com>
Date: 1997/01/15 Raw View
> markj@netaccess.co.nz wrote:
> > Why does C/C++ lack a binary number qualifier.
> > For example I would like to do this:
> >
> > unsigned char u = 01100011b;
surely you mean:
unsigned char u = 0b01100011;
> >
> > but instead I always have to use the hex version of 0x63 or invent some
> > horrible binary macro.
Chelly Green <chelly@eden.com> writes:
> Horrible? I just wrote something that allows this:
> unsigned char u = bin( 01100011 );
>
> Is that horrible? If it works, does it matter?
>
> // macro to allow entry of binary numbers
> // the sizeof part prevents non-binary digits from being used
> #define _bdig( dig, n ) ((n & (1L << (dig * 4))) >> (dig * 4 - dig))
> #define _bin( n ) int (_bdig(0,n)|_bdig(1,n)|_bdig(2,n)|_bdig(3,n)| \
> _bdig(4,n)|_bdig(5,n)|_bdig(6,n)|_bdig(7,n)+ \
> 0*(sizeof (int [(n&~0x11111111) == 0])))
>
> #define bin(num) _bin( 0x##num##L )
this macro does looks horrible.. you can make it look nicer(?) with
templates..
template <unsigned long N>
struct binval { enum { val = 2 * binval<N/16>::val + N%16
+ 0 * sizeof(int [N%16 < 2]) }; };
struct binval<0> { enum { val = 0 }; };
#define _bin(n) binval<n>::val
#define bin(n) _bin(0x ## n ## UL)
and now use bin(), as Chelly Green suggested..
but of course, that it works for only 8 bit numbers (if your machine
has 32 bit longs), reduces it to a mere toy..
abhishek
--
Abhishek Chauhan, abhishek.chauhan@sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/01/12 Raw View
markj@netaccess.co.nz writes:
>I've got a question which I have always wondered about regarding
>the C and C++ standards.
>
>Why does C/C++ lack a binary number qualifier. For example I would like
>to do this:
>
>unsigned char u = 01100011b;
>
>but instead I always have to use the hex version of 0x63 or invent some
>horrible binary macro.
An extension like this was proposed to the C++ standards committee,
but they rejected it. I don't know exactly why it was rejected,
but I believe one argument against it was that this is a "C" issue and
that the C++ committee ought to leave such things to the C committee.
I think this idea has been informally floated in the C committee as a
possibility for C9X, but I don't know whether anyone has written it up
as a formal proposal.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/01/13 Raw View
>Why does C/C++ lack a binary number qualifier. For example I would like
>to do this:
>
>unsigned char u = 01100011b;
>
>but instead I always have to use the hex version of 0x63 or invent some
>horrible binary macro.
>
>If there is no good reason why I should not be able to this then I would
>appreciate
>it if somebody in the know could propose it to the C/C++ standards committee.
The lexicon for binary integer constants was proposed to the ANSI C
committee (where you could specify, for example, 0b01101101), but it was
rejected by the committee. Apparently, they felt that octal and hex constants
were sufficient. (I disagree.)
They also rejected a proposal to allow underscores within numberic constants
(ala Ada). This, in my opinion, would improve legibility a great deal for
large constants. For example, 10000000 could be written as 10_000_000
instead, and 0xF7CA1E78 could be written as 0xF7CA_1E78.
Since the C++ committee is tracking the C standard and visa versa, it seems
unlikely that C++ would adopt these proposals either.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Michael R Cook <mcook@cognex.com>
Date: 1997/01/13 Raw View
>>>>> "CG" == Chelly Green <chelly@eden.com> writes:
CG> Maybe because hex is sufficient, or entering binary numbers is very
CG> error-prone.
It would be less error-prone if C/C++ were to allow underscores within numeric
literals (as some other languages do).
CG> 10000100 1000100 10001000 10000100
CG> How many different numbers are above? Takes a while to tell, huh? There
CG> are 3, one duplicated.
With
10_000_100 1_000_100 10_001_000 10_000_100
or, if you prefer
1000_0100 100_0100 1000_1000 1000_0100
it's easy to see which are the same.
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Chelly Green <chelly@eden.com>
Date: 1997/01/13 Raw View
markj@netaccess.co.nz wrote:
>
> Hi,
> I've got a question which I have always wondered about regarding
> the C and C++ standards.
>
> Why does C/C++ lack a binary number qualifier.
Maybe because hex is sufficient, or entering binary numbers is very
error-prone.
10000100 1000100 10001000 10000100
How many different numbers are above? Takes a while to tell, huh? There
are 3, one duplicated.
> For example I would like
> to do this:
>
> unsigned char u = 01100011b;
>
> but instead I always have to use the hex version of 0x63 or invent some
> horrible binary macro.
Horrible? I just wrote something that allows this:
unsigned char u = bin( 01100011 );
Is that horrible? If it works, does it matter?
> If there is no good reason why I should not be able to this then I would appreciate
> it if somebody in the know could propose it to the C/C++ standards committee.
>
> It would certainly make my programming life much easier.
Then use the macro! Here is the one I just wrote, with test program:
// free, public domain, use however you want
// macro to allow entry of binary numbers
// the sizeof part prevents non-binary digits from being used
#define _bdig( dig, n ) ((n & (1L << (dig * 4))) >> (dig * 4 - dig))
#define _bin( n ) int (_bdig(0,n)|_bdig(1,n)|_bdig(2,n)|_bdig(3,n)| \
_bdig(4,n)|_bdig(5,n)|_bdig(6,n)|_bdig(7,n)+ \
0*(sizeof (int [(n&~0x11111111) == 0])))
#define bin(num) _bin( 0x##num##L )
// can be in file scope, function scope, class scope
#define assert_anywhere( E ) struct { int chk [(E) != 0]; }
// check macro, demonstrate use
assert_anywhere( bin( 00000001 ) == 0x01 );
assert_anywhere( bin( 00000010 ) == 0x02 );
assert_anywhere( bin( 00000100 ) == 0x04 );
assert_anywhere( bin( 00001000 ) == 0x08 );
assert_anywhere( bin( 00010000 ) == 0x10 );
assert_anywhere( bin( 00100000 ) == 0x20 );
assert_anywhere( bin( 01000000 ) == 0x40 );
assert_anywhere( bin( 10000000 ) == 0x80 );
assert_anywhere( bin( 10001110 ) == 0x8E );
assert_anywhere( bin( 01110101 ) == 0x75 );
// should not compile because of non-binary digit
int i = bin( 20000000 );
--
Chelly Green | chelly@eden.com | C++ - http://www.eden.com/~chelly
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: markj@netaccess.co.nz
Date: 1997/01/12 Raw View
Hi,
I've got a question which I have always wondered about regarding
the C and C++ standards.
Why does C/C++ lack a binary number qualifier. For example I would like
to do this:
unsigned char u = 01100011b;
but instead I always have to use the hex version of 0x63 or invent some
horrible binary macro.
If there is no good reason why I should not be able to this then I would appreciate
it if somebody in the know could propose it to the C/C++ standards committee.
It would certainly make my programming life much easier.
Cheers,
+------------------------------------+---------------------------------------+
| EMBEDDED SYSTEMS SOFTWARE ENGINEER | Specialist In: |
| Mark Jordan TC, NZCE, BE, MIEEE | - DSP Applications |
| Top Quality Work, Reasonable Rates | - Microprocessor Applications |
| markj@netaccess.co.nz | - Reusable Software Libraries |
+------------------------------------+---------------------------------------+
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]