I'd like to represent an integer with a value between 0-100,000. Unsigned short int (range 0-65535) is too small. Unsigned int (range 0 - 4294967295) is big enough, but for a large data set this wastes a lot of space. Does there already exist a class that is in between?

I've thought of creating my own class:

class MediumInt
        // Member functions bitwise arithmetic
        // Member functions print integer value

        char aVal;
        int  bVal;

But, I'm not sure I could write something that was efficient enough. So, A) Does it already exists? B) If not, am I kinda on the right track for writing a class to fill the sweet spot between short int and int?

6 Years
Discussion Span
Last Post by Jsplinter

There is no data type between them. One thing you could do is pack the integers into an unsigned char buffer then unpack them when needed. That is going to be a huge performance problem though.


If you like to pack it effectively (but not without some penalty as warned by Ancient Dragon), you could use a bitset structure like

struct my_packed_data
    int32   my_3_byte_int        : 24,
            rest_for_other_scrap : 8;   

You could mix your 3-byte-int with other such constructs into a compact struct. On access, the compiler handles the expansion/packing for you. But be assured that the latter must be done and will cost a small tall.

The opposit way of doing is called "padding". If you like to primarily store data, packing is worth considering. If you like to calculate with data, expansion with padding is preferable. When using those packed structs for storing, expansion and padding is done automatically by the compiler.

Edited by White-Gandalf


A) Does it already exists?

No. Unless you are working on a very unusual platform that has a native word size of 3 bytes (most systems have a native word size of 4 or 8 bytes, and some really small embedded systems might use 2 bytes natively).

B) If not, am I kinda on the right track for writing a class to fill the sweet spot between short int and int?

No. The processor will not be able to deal with a 3 byte integer value, you will have to put it into a 4 byte integer variable if you want to do anything with it. Also, even if you make a class that stores the 3 bytes individually, there is little chance that size of the object will actually be 3 bytes, because most compilers will add padding in order to align the size of the object to the native word size of the system (this is a fundamental optimization that all compilers will do).

but for a large data set this wastes a lot of space

Sure. If you store many numbers using 4 bytes when 3 bytes would do just fine, then you get a 33% increase in the memory used. But, if you try to pack all the numbers such that they each use only 3 bytes, I can't even imagine how horribly slow your program will become. The additional memory expense is worth the performance gain from using native word sizes, this is a no-brainer (and that is why compilers make that decision for you).

If you are really concerned with the size, you should use a compression algorithm to compress the data or the parts of it that you are not using. This going to be much better, both in terms of performance and memory usage.

Votes + Comments
Solid explanations!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.