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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:17:41+00:00 2026-05-25T15:17:41+00:00

Possible Duplicate: Creating multiple numbers with certain number of bits set I’m attempting to

  • 0

Possible Duplicate:
Creating multiple numbers with certain number of bits set

I’m attempting to write some code which will put each possible combination of numbers in an array by shifting the bits across.

For example, I wanted to find all possible combinations of 3 bits (where the max a digit can be is 6) the array should contain:

000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011

And so on…

From what I’ve interpreted, when the last position bit is 1 we shift the number by 1 (x >> 1) and add a 1 at the start. However, I’m unsure how to code the rest. I’m using C to write this.

Also – as far as I can tell this is a colex sequence, however, I’m all ears if there is another sequence that will give me the same end result (array with all possible combinations of k-bits with a constraint of N).

  • 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-25T15:17:41+00:00Added an answer on May 25, 2026 at 3:17 pm

    You can solve this by generating the sequences recursively.

    Let us define a recursive function f(int index, int bits, int number) that will take in the current index of the bit and the number of bits left to place, and the number generated so far. Then, you have the option of setting the current bit to 1 or to 0, and recursing from there.

    Overall, the time complexity should be O(number of sequences), or O(N choose B), where N is the number of digits and B is the number of bits set to 1.

    The function goes something like this:

    void f(int index, int bits, int number) {
        if (index == 0) {
            if (bits == 0) {   // all required bits have been used
                emit_answer(number); // chuck number into an array, print it, whatever.
            }   
            return;
        }   
    
        if (index-1 >= bits) {  // If we can afford to put a 0 here
            f(index-1, bits, number);
        }   
    
        if (bits > 0) {  // If we have any 1s left to place
            f(index-1, bits-1, number | (1 << (index-1)));
        }   
    }
    
    // to call:
    f(6, 3, 0); 
    

    For N,B = 6,3 the output matches yours, and is in sorted order. Link to working example: http://codepad.org/qgd689ZM

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

Sidebar

Related Questions

Possible Duplicate: Generating Random Numbers in Objective-C Hi I am creating an app where
Possible Duplicate: C++ can I reuse fstream to open and write multiple files? why
Possible Duplicate: iPhone - file properties Hi all. i m creating an application which
Possible Duplicate: How to set AUTO-SCROLLING of JTextArea in Java GUI? I'm creating an
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: Creating a custom Document Library in SharePoint I have 2 users, Martin
Possible Duplicate: Is there a clean way to prevent windows.h from creating a near
Possible Duplicate: How to open-source an application that uses API keys I'm creating an
Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method What's the difference between creating a prototype like

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.