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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:54:26+00:00 2026-06-11T10:54:26+00:00

How do I generate all possible combinations of n-bit strings? I need to generate

  • 0

How do I generate all possible combinations of n-bit strings? I need to generate all combinations of 20-bit strings in a fastest way possible. (my current implementation is done with bitwise AND and right shift operation, but I am looking for a faster technique).

I need to store the bit-strings in an array (or list) for the corresponding decimal numbers, like —

0 --> 0 0 0

1 --> 0 0 1

2 --> 0 1 0 … etc.

any idea?

  • 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-11T10:54:27+00:00Added an answer on June 11, 2026 at 10:54 am
    for (unsigned long i = 0; i < (1<<20); ++i) {
        // do something with it
    }
    

    An unsigned long is a sequence of bits.

    If what you want is a string of characters '0' and '1', then you could convert i to that format each time. You might be able to get a speed-up taking advantage of the fact that consecutive numbers normally share a long initial substring. So you could do something like this:

    char bitstring[21];
    for (unsigned int i = 0; i < (1<<10); ++i) {
        write_bitstring10(i, bitstring);
        for (unsigned int j = 0; j < (1<<10); ++j) {
            write_bitstring10(j, bitstring + 10);
            // do something with bitstring
        }
    }
    

    I’ve only increased from 1 loop to 2 there, but I do a little over 50% as much converting from bits to chars as before. You could experiment with the following:

    • use even more loops
    • split the loops unevenly, maybe 15-5 instead of 10-10
    • write a function that takes a string of zeros and ones, and adds 1 to it. It’s pretty easy: find the last '0', change it to a '1', and change all the '1's after it to '0'.

    To fiendishly optimize write_bitstring, multiples of 4 are good because on most architectures you can blit 4 characters at a time in a word write:

    To start:

    assert(CHAR_BIT == 8);
    uint32_t bitstring[21 / 4]; // not char array, we need to ensure alignment
    ((char*)bitstring)[20] = 0; // nul terminate
    

    Function definition:

    const uint32_t little_endian_lookup = {
        ('0' << 24) | ('0' << 16) | ('0' << 8) | ('0' << 0),
        ('1' << 24) | ('0' << 16) | ('0' << 8) | ('0' << 0),
        ('1' << 24) | ('1' << 16) | ('0' << 8) | ('0' << 0),
        // etc.
    };
    // might need big-endian version too
    
    #define lookup little_endian_lookup // example of configuration
    
    void write_bitstring20(unsigned long value, uint32_t *dst) {
        dst[0] = lookup[(value & 0xF0000) >> 16];
        dst[1] = lookup[(value & 0x0F000) >> 12];
        dst[2] = lookup[(value & 0x00F00) >> 8];
        dst[3] = lookup[(value & 0x000F0) >> 4];
        dst[4] = lookup[(value & 0x0000F)];
    }
    

    I haven’t tested any of this: obviously you’re responsible for writing a benchmark that you can use to experiment.

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

Sidebar

Related Questions

Can someone please tell how to generate n-bit strings(all possible combinations) i.e. counting bits
Given a positive integer n, I want to generate all possible n bit combinations
How can I generate all possible bit combinations in an array of bits of
I'm trying to generate all possible combinations for pair of 1's within given bit
I need c++ code which will generate all possible combinations (n,k) with repitions where
Algorithm to generate all possible letter combinations of given string down to 2 letters
I am trying to generate a list of all possible number combinations within a
I need to generate Entities/Object from selected tables - not all. Is this possible
I need to generate all combinations (not permutations) in VB .NET, I've been search
Possible Duplicate: Generating all Possible Combinations Is there a good LINQ way to do

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.