Hi,

On Sat, Aug 16, 2008 at 10:32 PM, Zeljko Vrba <zvrba@ifi.uio.no> wrote:
Integer types in C and C++ are a mess.  For example, I have made a library
where a task is identified by a unique unsigned integer.
==

Another example: an external library defines its interfaces with signed integer
types, I work with unsigned types (why? to avoid even more
==

Does anyone know about an integer class that lets the user define the number of
bits used for storage, lower allowed bound and upper allowed bound for the
range?  Like: template<int BITS, long low, long high> class Integer;

I would like to refer you to section C.6.1 of "The C++ Programming Language, 3rd Ed." by Bjarne Stroustrup where the integer promotions are described. Elsewhere in the same pages, other conversions are also described.

Basically, Stroustrup suggests to use integer types ONLY in the very lowest level code and use higher level types that you defined according to your own problem domain in your design.

If you do want to use integer types then stick to plain ints and plain chars - as much as you possibly can. Use unsigned ints or signed char or unsigned char or longs and you might run into portability issues across different compilers/platforms, afaik, when you mix all of these types in calculations, for example.

Lastly, a bit-field (if defined in a structure, that is) gets converted to an int if int can appropriately represent it otherwise it gets converted to an unsigned int. To save you time, you can use the "bitset" class of standard C++ for your purpose.

-Asif