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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:09:58+00:00 2026-06-02T17:09:58+00:00

I have a (cyclic) bitset<N> and would like to retrieve the substring i…i+K-1 (where

  • 0

I have a (cyclic) bitset<N> and would like to retrieve the substring i...i+K-1 (where it could happen that i = N - 1; K = 5, so it has to wrap around and get N-1; 0; 1; 2; 3), as another bitset<K> (K is of course known at compile-time)

The obvious thing doesn’t work because operator & doesn’t allow operands of different sizes (although it would be trivial?)

bitset<N> data = ...;
bitset<K> mask; mask = ~mask;
bitset<K> rotated = in << i | in >> (K - i);
bitset<K> slice = rotated & mask;

The next best thing doesn’t work with large N

bitset<K> slice( rotated.to_ullong() & mask.to_ullong() );

What to do, short of implementing bitset<min<N,K>::value> operator &(bitset<N>,bitset<K>)? (and maybe not as terribly inefficient, this approach copies the set 3 times)

  • 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-02T17:10:00+00:00Added an answer on June 2, 2026 at 5:10 pm

    As you’ve seen none of the bitset public member functions support copying between bitsets of different sizes. The convert to ulong function isn’t useful if you have sets that won’t fit in a ulong. I don’t see why the standard couldn’t at least have a copy constructor and assignment operator that supported different sizes. That said given the standard class I don’t see any way around copying the bits one at a time.

    I’d suggest something like the following:

    template⟨size_t D_SZ,size_t S_SZ⟩
    void CopyBitset(std::bitset⟨D_SZ⟩ &dest, const std::bitset⟨S_SZ⟩ &source,size_t idx,
        size_t count,size_t destidx=0)
    {
            for(size_t i = 0; i != count;++i){
                    dest.set((i + destidx) % D_SZ, source[(i + idx) % S_SZ]);      
            }
    }
    

    At least the shift and copy operations are combined.
    You might also take a look at boost’s dynamic bitset to see if it would more closely match your usage .

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

Sidebar

Related Questions

Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
I am having a requirement like want to have a cyclic gallery view shown
I am writing some code that traverses a structure that may have cyclic references.
We have a rule that disallows cyclic dependencies between packages. We also have a
I have fancybox set up so that it displays my pdf, however I would
I have a class that represents the state of the world. The class has
Thing is I have a file that has room for metadata. I want to
I have a case that I have the following cyclic dependencies in maven: JAR

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.