Article View: comp.std.c++
Article #3374Re: Numeric literal formats
From: Kevlin A P Henney
Date: Tue, 20 Feb 1996 18:55
Date: Tue, 20 Feb 1996 18:55
71 lines
3049 bytes
3049 bytes
In article <3125C1DC.35AE@ix.netcom.com>
pderocco@ix.netcom.com "Paul D. DeRocco" writes:
> Why, oh why, aren't we allowed to write binary numbers in C++? The
> standard could trivially be extended by allowing binary numbers to be
> written, prefixed with 0b, or perhaps 0y. This makes bit masks _much_
> easier to read. And it wouldn't break any presently legal program.
You might like to try the following:
template<int digit> struct bit;
struct bit<0> { enum { value = 0 }; };
struct bit<1> { enum { value = 1 }; };
template<int digits> struct bin;
struct bin<0> { enum { value = 0 }; };
template<int digits> struct bin
{
enum
{
value = bin<digits / 10>::value * 2 +
bit<digits % 10>::value
};
};
Used as
bin<1101>::value
bin<11001100>::value
Catches as illegal
bin<12345>::value
bin<011001>::value
If nothing else, the code is a good source of amusement -- present it to your
colleagues w/ the names changed to perplex the innocent :-)
Seriously, this is practical w/i the constraints of 32 bit ints and <= 10 bit
bit specs, and for compilers using the old specialization syntax (ie. the
majority).
> Another related improvement would be to allow embedded underscores in
> numbers, for clarity. 1_000_000_000 is obviously a billion, while
> 1000000000 is not so obviously a billion. Same for 0x8000_0000, or
> 0y0001_1110_0000_0000. Again, it doesn't break anything.
Alternatively we could try for space separation and handle adjacent literals
as one, as is done w/ string literals:
1 000 000 000
0x 8000 0000
0.12345 67890
:->
_______________________________________________________________________
Kevlin A P Henney 2sdg Ltd kevlin@two-sdg.demon.co.uk
"What happens to the hole when the cheese is gone?" Bertolt Brecht
_______________________________________________________________________
---
[ To submit articles: Try just posting with your newsreader. 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
]
Technical Details
Message-ID:
<824739573snz@two-sdg.demon.co.uk>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<3125C1DC.35AE@ix.netcom.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: Kevlin A P Henney <Kevlin@two-sdg.demon.co.uk> Newsgroups: comp.std.c++ Subject: Re: Numeric literal formats Date: 20 Feb 1996 10:55:25 PST Organization: 2sdg Ltd Approved: austern@isolde.mti.sgi.com Message-ID: <824739573snz@two-sdg.demon.co.uk> References: <3125C1DC.35AE@ix.netcom.com> Reply-To: kevlin@two-sdg.demon.co.uk NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Mon, 19 Feb 96 14:19:33 GMT X-Newsreader: Demon Internet Simple News v1.29 X-Mail2News-Path: two-sdg.demon.co.uk X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMSoZL0y4NqrwXLNJAQF0LwH+IzgCcJcvAfslhcdEX+/0Jb67woXJwLvp RunnmC0iBijB09VXRGMKXWuK3E1vBVu/2LD8bQN04rfCSKcF5hGFUw== =iRFG Originator: austern@isolde.mti.sgi.com