Jump to content
  • Advertisement
Sign in to follow this  
gohkgohk

Is it possible to make a custom type of integer?

This topic is 3820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
What programming language? Or do you mean in circuit design in creating a brand new processor just for your program?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!