Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:58:32+00:00 2026-06-11T16:58:32+00:00

I am trying a resource insensitive functionality in C++. I am implementing an array

  • 0

I am trying a resource insensitive functionality in C++. I am implementing an array which has 10000 records but any record will have only possible 3 values i.e. 0,1,2. So i was wondering instead of storing memory for 10000 instance all 3 together if some how I can just save one instance of each and manage logically. Not sure how exactly to implemenet.

For example my array would be something like this.

{1, 0, 0, 1, 2, 1, 1, 1, 0, 2, 2, 0, 0, 0, 2,………….}

we might go for more than 10000 records too

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:58:33+00:00Added an answer on June 11, 2026 at 4:58 pm

    It sounds like you could just create an array of 2500 bytes, with 4 values per byte (each value takes 2 bits). Access any single value using bit-shifting/masking. I suspect that’s going to be simpler than a scheme which groups the values, and will be more “array-like” for access. Of course, it’s hard to say for sure as we don’t know what you need to do with the values.

    You could actually fit 5 values into each byte (as 35 is 243) so you’d only need a byte array of size 2000… but the access code would be somewhat trickier. I would resist this extra complexity unless you really need it.

    Additionally, if the values are relatively sparse – e.g. almost everything is 0, with just a few 1s and 2s – then you could obviously store that more efficiently.

    EDIT: Okay, so I haven’t done any C++ for a long time, but it would be something like:

    // Entirely untested. Please test thoroughly, and make sure you understand it
    // before using it.
    int get_value(unsigned index)
    {
        // TODO: Argument validation
        unsigned raw_index = index / 4;
        unsigned index_within_byte = (index % 4) * 2;
    
        return (array[raw_index] >> index_within_byte) & 3;
    }
    
    void set_value(unsigned index, int value)
    {
        // TODO: Argument validation
        unsigned raw_index = index / 4;
        unsigned index_within_byte = (index % 4) * 2;
    
        int mask = 0xff ^ (3 << index_within_byte);
        array[raw_index] = (array[raw_index] & mask) | (value << index_within_byte);
    }
    

    EDIT: Thinking about it further, you might even want to create an array of uint32_t or uint64_t instead of bytes, and put 16 or 32 “real” values into each array element. I suspect that on most processors that may make for more efficient memory access.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to append strings to a local resource file but I
I'm trying to add a resource, which is a standard IValueConverter instance. I want
I have been trying to find some resource on this topic for a while
I am trying to access a resource file from a servlet but getting HTTP
I'm trying to use the Resource Governor in SQL Server 2008 but I find
I'm trying to find a resource that has benchmarks of Dojo, jquery, mootools, etc
i am trying to store some settings in resource of my application but failed
I am trying to read a resource of an XML file but I am
I have spent hours trying to find a resource that explains the process of
I'm trying to compile a resource file (.rc) for a Win32 application but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.