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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:50:04+00:00 2026-06-16T22:50:04+00:00

I have a bit-mask of N chars in size, which is statically known (i.e.

  • 0

I have a bit-mask of N chars in size, which is statically known (i.e. can be calculated at compile time, but it’s not a single constant, so I can’t just write it down), with bits set to 1 denoting the “wanted” bits. And I have a value of the same size, which is only known at runtime. I want to collect the “wanted” bits from that value, in order, into the beginning of a new value. For simplicity’s sake let’s assume the number of wanted bits is <= 32.

Completely unoptimized reference code which hopefully has the correct behaviour:

template<int N, const char mask[N]>
unsigned gather_bits(const char* val)
{
    unsigned result   = 0;
    char*    result_p = (char*)&result;
    int      pos      = 0;
    for (int i = 0; i < N * CHAR_BIT; i++)
    {
        if (mask[i/CHAR_BIT] & (1 << (i % CHAR_BIT)))
        {
            if (val[i/CHAR_BIT] & (1 << (i % CHAR_BIT)))
            {
                if (pos < sizeof(unsigned) * CHAR_BIT)
                {
                    result_p[pos/CHAR_BIT] |= 1 << (pos % CHAR_BIT);
                } 
                else
                {
                    abort();
                }
            }
            pos += 1;
        }
    }
    return result;
}

Although I’m not sure whether that formulation actually allows access to the contents of the mask at compile time. But in any case, it’s available for use, maybe a constexpr function or something would be a better idea. I’m not looking here for the necessary C++ wizardry (I’ll figure that out), just the algorithm.

An example of input/output, with 16-bit values and imaginary binary notation for clarity:

mask   = 0b0011011100100110
val    = 0b0101000101110011
--
wanted = 0b__01_001__1__01_ // retain only those bits which are set in the mask
result = 0b0000000001001101 // bring them to the front
                   ^ gathered bits begin here

My questions are:

  • What’s the most performant way to do this? (Are there any hardware instructions that can help?)

  • What if both the mask and the value are restricted to be unsigned, so a single word, instead of an unbounded char array? Can it then be done with a fixed, short sequence of instructions?

  • 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-16T22:50:05+00:00Added an answer on June 16, 2026 at 10:50 pm

    There will pext (parallel bit extract) that does exactly what you want in Intel Haswell. I don’t know what the performance of that instruction will be, probably better than the alternatives though. This operation is also known as “compress-right” or simply “compress”, the implementation from Hacker’s Delight is this:

    unsigned compress(unsigned x, unsigned m) {
       unsigned mk, mp, mv, t; 
       int i; 
    
       x = x & m;           // Clear irrelevant bits. 
       mk = ~m << 1;        // We will count 0's to right. 
    
       for (i = 0; i < 5; i++) {
          mp = mk ^ (mk << 1);             // Parallel prefix. 
          mp = mp ^ (mp << 2); 
          mp = mp ^ (mp << 4); 
          mp = mp ^ (mp << 8); 
          mp = mp ^ (mp << 16); 
          mv = mp & m;                     // Bits to move. 
          m = m ^ mv | (mv >> (1 << i));   // Compress m. 
          t = x & mv; 
          x = x ^ t | (t >> (1 << i));     // Compress x. 
          mk = mk & ~mp; 
       } 
       return x; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way with which I can generate bit mask of a number
If I have an image (IplImage 8-bit) and a binary mask (which is also
I have a small problem. After making a hex mask, I can not copy/paste
I have a bit of a problem that I can't seem to code my
I have read through this SO question about 32-bits, but what about 64-bit numbers?
I have a buffer with some data which come in different bit sizes (8
I have a function which checks, whether a bit in an int is set
Okay, got a question. I have assembled a bit mask for options. Basically my
i have a three dimensional array which i am using as a bit table
I have a bit shift mask that represents days in a week: Sunday =

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.