Topic: Numeric literal formats


Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1996/02/17
Raw View
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.

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.

Oh heck, I might as well throw this in. Why isn't there an escape for
escape? That is, why can't \e be defined as \x1B, since it's such a
common control character?

--

Ciao,
Paul D. DeRocco
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]





Author: Michael Cook <mcook@cognex.com>
Date: 1996/02/19
Raw View
>>>>> "P" == "Paul D DeRocco" <pderocco@ix.netcom.com> writes:

 P> Oh heck, I might as well throw this in. Why isn't there an escape for
 P> escape? That is, why can't \e be defined as \x1B, since it's such a
 P> common control character?

What would \e mean if the character set were not ASCII?
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]





Author: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
Date: 1996/02/20
Raw View
In article <r4g2c8lcrp.fsf@erawan.cognex.com> Michael Cook
<mcook@cognex.com> writes:

|> >>>>> "P" == "Paul D DeRocco" <pderocco@ix.netcom.com> writes:

|>  P> Oh heck, I might as well throw this in. Why isn't there an escape for
|>  P> escape? That is, why can't \e be defined as \x1B, since it's such a
|>  P> common control character?

|> What would \e mean if the character set were not ASCII?

I was about to post the same thing, then it occured to me that this
argument is equally valid concerning \a or \v (or for that matter,
probably everything but \n).

The other control characters are defined in terms of their semantics,
and not the code.  I'm not sure how the standard could define the
semantics of escape; this may be the reason why it isn't there.

--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
                -- A la recherche d'une activiti dans une region francophone
---
[ 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: Kevlin A P Henney <Kevlin@two-sdg.demon.co.uk>
Date: 1996/02/20
Raw View
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
]