Topic: Bit fumbling ("byte" in C++?)
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 27 Dec 1994 14:31:25 GMT Raw View
In article <CABO.94Dec19150859@ruin.informatik.uni-bremen.de>, cabo@informatik.uni-bremen.de (Carsten Bormann) writes:
|> How about a system specific header (the example is for a Sun):
[..]
|> Now I can write
|>
|> | #include <bit.h>
|> |
|> | bit<16>::signed_int i;
|>
[..]
Looks like a good idea to me. My experience shows that in many places
one makes assumptions about the number of bits or the maximum number
an integral type can hold, that are not garanteed by the language.
It is clear that a bit<16>::unsigned int has 16 bits and the
maximum storable number is 2 pow 16 - 1.
Given a header like this proposed bit.h it would be easier and thus
more likely that one produces code without implicit assumptions
about the bitwidth of basetypes.
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Institut f r Informatik, Technische Universit t M nchen.
email: schuenem@informatik.tu-muenchen.de
Author: cabo@informatik.uni-bremen.de (Carsten Bormann)
Date: 19 Dec 1994 14:08:59 GMT Raw View
In article <INKARI.94Dec19022146@lk-hp-7.hut.fi>
inkari@snakemail.hut.fi (Juha Inkari) writes:
> #include <limits.h>
>
> #if USHRT_MAX == 65535
> typedef unsigned short u16bits;
> #else
> #error Get a compiler that supports 16 bit native types.
> #endif
How about a system specific header (the example is for a Sun):
| template<int i> class bit { }; // empty
|
| struct bit<8> {
| typedef unsigned char unsigned_int;
| typedef signed char signed_int;
| };
|
| struct bit<16> {
| typedef unsigned short unsigned_int;
| typedef signed short signed_int;
| };
|
| struct bit<32> {
| typedef unsigned long unsigned_int;
| typedef signed short signed_int;
| typedef float signed_float;
| };
|
| struct bit<64> {
| typedef unsigned long long unsigned_int;
| typedef signed long long signed_int;
| typedef double signed_float;
| };
Now I can write
| #include <bit.h>
|
| bit<16>::signed_int i;
And, of course, a program that uses an unavailable size
| #include <bit.h>
|
| bit<24>::signed_int i;
will (correctly) be rejected by the compiler.
(Of course, this is a derivation of the numeric_limits class that has
been adopted by ISO/IEC JTC1/SC22/WG21.)
If you want, you could write a "portable" bit.h (with the exception of
long long) by using the same technique given in the referenced
article, but I'd rather want it in the standard library...
Gruesse, Carsten