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

  • Home
  • SEARCH
  • 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 6572183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:58:25+00:00 2026-05-25T14:58:25+00:00

std::bitset has a to_string() method for serializing as a char -based string of 1

  • 0

std::bitset has a to_string() method for serializing as a char-based string of 1s and 0s. Obviously, this uses a single 8 bit char for each bit in the bitset, making the serialized representation 8 times longer than necessary.
I want to store the bitset in a binary representation to save space. The to_ulong() method is relevant only when there are less than 32 bits in my bitset. I have hundreds.
I’m not sure I want to use memcpy()/std::copy() on the object (address) itself, as that assumes the object is a POD.

The API does not seem to provide a handle to the internal array representation from which I could have taken the address.

I would also like the option to deserialize the bitset from the binary representation.

How can I do this?

  • 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-05-25T14:58:26+00:00Added an answer on May 25, 2026 at 2:58 pm

    This is a possible approach based on explicit creation of an std::vector<unsigned char> by reading/writing one bit at a time…

    template<size_t N>
    std::vector<unsigned char> bitset_to_bytes(const std::bitset<N>& bs)
    {
        std::vector<unsigned char> result((N + 7) >> 3);
        for (int j=0; j<int(N); j++)
            result[j>>3] |= (bs[j] << (j & 7));
        return result;
    }
    
    template<size_t N>
    std::bitset<N> bitset_from_bytes(const std::vector<unsigned char>& buf)
    {
        assert(buf.size() == ((N + 7) >> 3));
        std::bitset<N> result;
        for (int j=0; j<int(N); j++)
            result[j] = ((buf[j>>3] >> (j & 7)) & 1);
        return result;
    }
    

    Note that to call the de-serialization template function bitset_from_bytes the bitset size N must be specified in the function call, for example

    std::bitset<N> bs1;
    ...
    std::vector<unsigned char> buffer = bitset_to_bytes(bs1);
    ...
    std::bitset<N> bs2 = bitset_from_bytes<N>(buffer);
    

    If you really care about speed one solution that would gain something would be doing a loop unrolling so that the packing is done for example one byte at a time, but even better is just to write your own bitset implementation that doesn’t hide the internal binary representation instead of using std::bitset.

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

Sidebar

Related Questions

For an std::map<std::string, std::string> variables , I'd like to do this: BOOST_CHECK_EQUAL(variables[a], b); The
I have a std::bitset that I'd like to write to a file, bit for
I convert a character to binary using std::bitset<CHAR_BIT> binary('c'); but this doesn't work for
std::string str = string: const char* cstr = str.c_str(); str.clear(); //here, check if cstr
I have byte to binary string function, std::string byte_to_binary(unsigned char byte) { int x
For std::map, how will insert behave if it has to resize the container and
Is std::string size() a O(1) operation? The implementation of STL I'm using is the
I have a std::multimap where key is a custom class. Something like this: Class
I have a class with a static std::map member variable that maps char s
I can have std::bitset< 10 > bitsetA; or const size_t LengthB = 20; std::bitset<

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.