Is it possible to make a custom type of integer?

Started by
19 comments, last by Zahlman 16 years, 2 months ago
if i know that i will only store a list of integers between -10 & 10, so there will be a lot of unwanted memory space wasted if i create an integer type for them. Is it possible to make a custom type of integer that only let you to store integers between -10 & 10, so no space will be wasted?
Advertisement
What programming language? Or do you mean in circuit design in creating a brand new processor just for your program?

If it's C/C++ you could use bitfields in a structure like this:

struct SmallIntEntry
{
int value1 : 5;
int value2 : 5;
int value3 : 5;
};

With correct alignment, sizeof( SmallIntEntry) will be 2 bytes for 3 values.
Creating a list of this will be kind of nasty though.
How many integers are you storing and how much RAM do you have to work with?
Such concerns are usually only pertinent to embedded systems.
C++
Then the answer is no. Data types in C++ must always have a size of at least 1, which is minimally 8 bits. It's impossible to create a data type smaller than this.
Quote:Original post by SiCrane
Then the answer is no. Data types in C++ must always have a size of at least 1, which is minimally 8 bits. It's impossible to create a data type smaller than this.

Oh...
:(
But wt is the meaning of PsyQo's reply?
i want to create a very big array(100000) which only store integers between -10 to 10.
something to consider your 100000 entity array still takes up less then 1/5th of a megabyte you still be able to run it on an old 486 with 4mb ram with out having to worry about memory. even on the gba you would be fine with memory so you could create the array using char instead of an int so it only takes up 1/10th of a ~ megabyte
0))))))>|FritzMar>
PsyQo's post concerns bitfields, which are variables that are part of a data type. You can't use a bitfield as a type, but you can create a type that contains bitfields. However, such a data type will always use at least one byte.
Quote:Original post by gohkgohk
Is it possible to make a custom type of integer that only let you to store integers between -10 & 10, so no space will be wasted?

Well, what type are you using now? With signed char, you'd only waste 3 bits.

This topic is closed to new replies.

Advertisement