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 4552380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:46:48+00:00 2026-05-21T16:46:48+00:00

I want to concat a big bitset with a smaller one in a way

  • 0

I want to concat a big bitset with a smaller one in a way that wont kill performance. Currently my application spends 20% of cpu time in just the following code:

boost::dynamic_bitset<> encode(const std::vector<char>& data)
{
    boost::dynamic_bitset<> result;

    std::for_each(data.begin(), data.end(), [&](unsigned char symbol)
    {
        for(size_t n = 0; n < codes_[symbol].size(); ++n)
            result.push_back(codes_[symbol][n]); // codes_[symbol][n].size() avarage ~5 bits
    });
    return result;
}

I have read this post which proposes a solution, which unfortunately will not work for me as the size difference between the sizes of destination bitset and the source bitset is very large.

Any ideas?

If this is not possible to do efficiently with boost::dynamic_bitset then I’m open for other suggestions.

  • 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-21T16:46:49+00:00Added an answer on May 21, 2026 at 4:46 pm

    I wrote my own bitset class. I appreciate any suggestions for improvements. I will try to look into SSE and see if there is anything useful there.

    With my very rough benchmark I got a 11x performance increase while appending 6 bits at a time.

    class fast_bitset
    {
    public:
        typedef unsigned long block_type;
        static const size_t bits_per_block = sizeof(block_type)*8;
    
        fast_bitset() 
            : is_open_(true)
            , blocks_(1)
            , space_(blocks_.size()*bits_per_block){}
    
        void append(const fast_bitset& other)
        {
            assert(!other.is_open_);
    
            for(size_t n = 0; n < other.blocks_.size()-1; ++n)
                append(other.blocks_[n], bits_per_block);
            append(other.blocks_.back() >> other.space_, bits_per_block - other.space_);
        }
    
        void append(block_type value, size_t n_bits)
        {
            assert(is_open_);
            assert(n_bits < bits_per_block);
    
            if(space_ < n_bits)
            {
                blocks_.back() = blocks_.back() << space_;
                blocks_.back() = blocks_.back() | (value >> (n_bits - space_));
                blocks_.push_back(value);
                space_ = bits_per_block - (n_bits - space_);
            }
            else
            {
                blocks_.back() = blocks_.back() << n_bits;
                blocks_.back() = blocks_.back() | value;
                space_ -= n_bits;
            }
        }
    
        void push_back(bool bit)
        {
            append(bit, 1);
        }
    
        bool operator[](size_t index) const
        {
            assert(!is_open_);
    
            static const size_t high_bit = 1 << (bits_per_block-1);
            const size_t block_index = index / bits_per_block;
            const size_t bit_index = index % bits_per_block;
            const size_t bit_mask = high_bit >> bit_index;
            return blocks_[block_index] & bit_mask;
        }
    
        void close()
        {
            blocks_.back() = blocks_.back() << space_;
            is_open_ = false;
        }
    
        size_t size() const
        {
            return blocks_.size()*bits_per_block-space_;
        }
    
        const std::vector<block_type>& blocks() const {return blocks_;}
    
        class reader
        {
        public:
            reader(const fast_bitset& bitset) 
                : bitset_(bitset)
                , index_(0)
                , size_(bitset.size()){}
            bool next_bit(){return bitset_[index_++];}
            bool eof() const{return index_ >= size_;}
        private:
            const fast_bitset& bitset_;
            size_t index_;
            size_t size_;
        };
    
    private:
        bool is_open_;
        std::vector<block_type> blocks_;
        size_t space_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two long type columns that I want to concat during sql query.
I have a boolean array (length = 2) that I want to concat and
I have a datatble with one column I want to concat each filename with
I want to concat two Columns in the TableAdapter Wizard of the VS Dataset-Designer.
here i want to run this program and take one character pointer like char
i want update my row and concat my string but i have an error
I want to do something like this, but it seems that i just dont
Imagine you would want to select all elements of one sequence all , except
I want to concat two text and display with single TextView in Android. I
In PostgreSQL I want to concat the current_timestamp with an interval as follows: select

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.