0

Let's say memory is precious, and I have a class with a uint32_t member variable ui and I know that the values will stay below 1 million. The class also hase some bool members.

  1. Does it make sense to use the highest (highest 2,3,..) bit(s) of ui in order to save memory, since bool is 1 byte?

  2. If it does make sense, what is the most efficient way to get the highest (leftmost?) bit (or 2nd)? I read a few old threads and there seems to be disagreement about using inline ASM or some sort of shift.

8
  • 1
    C bitfield syntax is fully supported in C++. Commented Jan 12, 2021 at 15:22
  • 1
    Bit fields are probably the best solution, until you can manage to have a better optimized implementation than the compiler. Commented Jan 12, 2021 at 15:25
  • struct A { bool a:1; uint32_t b: 31;}; Commented Jan 12, 2021 at 15:31
  • Memory is precious but likely not that much. Commented Jan 12, 2021 at 15:39
  • @VictorGubin that will align b because it is not of the same type as a. Maybe a compiler-specific member backing directive would help. Commented Jan 12, 2021 at 15:40

2 Answers 2

1

It's a bit dangerous to use part of the bits as bool. The thing is that the way the numbers are kept in binary, makes it harder to maintain that keeping mechanism correct.

Negative numbers are kept as a complement of positive. Check this for more explanation. You may assign number to be 10 and then setting bool bit from false to true, and the number may turn out to become huge negative number as a result.

As for getting if n-th bit is 0 or 1 you can use this, where 0-th bit is the right most:

int nth_bit(int a, int n){
    return a & (1 << n);
}

It will return 0 or 1 identifying the n-th bit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I realize using a variable for two purposes comes with drawbacks, so I'm actually not convinced this is a good idea myself. And thanks for your solution, but I would think that for a 4-byte variable, your solution requires a lot of shifts. :/
1

Well, if the memory is in fact precious, you should look deeper.

1,000,000 uses only 20 bits. This is less that 3 bytes. So you can allocate 3 bytes to keep your value and up to four booleans. Obviously, access will be a bit more complicated, but you save 25% of memory!

If you know that the values are below 524,287, for example, you can save another 15% by packing it (with bool) into 20 bits :)

Also, keeping bool in a separate array (as you said in a comment) would kill performance if you need to access the value and a corresponding bool simultaneously because they are far apart and will likely never be in a cache.

2 Comments

Thank you for your reply! As I've said elsewhere I have come to realize that while this has been suggested to me by a C++ veteran, combining variables does not make sense in my case. The value itself is also accessed frequently (the bool not so much), so each time I'd have to calculate the corresponding value, which does not come for free. :/
@Markstar - eternal dilemma: save time or save RAM? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.