Is it possible to use std :: bitset <4> to store a decimal digit?
Yes, in principle, a good idea. This is a well-known optimization called BCD .
(actually decimal digits). What is a spatial efficient way to store such data?
You can write a representation of a decimal digit using one piece of a busy byte. Maths, optimized, or ASCII representations of numbers or those may also be applied.
std::bitset<4> will not be used to compress data.
std::bitset<4> will still occupy the full byte.
An alternative data structure that I could think of is a bitfield
// Maybe
There is even a library to convert this format to a normalized numerical format supported in your target CPU architecture:
Another way I could think of is to write my own class:
class BCDEncodedNumber { enum class Sign_t : char { plus = '+' , minus = '-' }; std::vector<uint8_t> doubleDigitsArray; public: BCDEncodedNumber() = default; BCDEncodedNumber(int num) { AddDigits(num);
source share