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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:02+00:00 2026-05-15T03:45:02+00:00

I’ve been struggling to wrap my head around this for some reason. I have

  • 0

I’ve been struggling to wrap my head around this for some reason. I have 15 bits that represent a number. The bits must match a pattern. The pattern is defined in the way the bits start out: they are in the most flush-right representation of that pattern. So say the pattern is 1 4 1. The bits will be:

000000010111101

So the general rule is, take each number in the pattern, create that many bits (1, 4 or 1 in this case) and then have at least one space separating them. So if it’s 1 2 6 1 (it will be random):

001011011111101

Starting with the flush-right version, I want to generate every single possible number that meets that pattern. The # of bits will be stored in a variable. So for a simple case, assume it’s 5 bits and the initial bit pattern is: 00101. I want to generate:

00101
01001
01010
10001
10010
10100

I’m trying to do this in Objective-C, but anything resembling C would be fine. I just can’t seem to come up with a good recursive algorithm for this. It makes sense in the above example, but when I start getting into 12431 and having to keep track of everything it breaks down.

  • 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-15T03:45:03+00:00Added an answer on May 15, 2026 at 3:45 am

    Building on @Mark Byers’s and Moron’s answers your task can be reformulated as follows:

    Enumerate all ways to put K zeros into N places (see combinations with repetition and Stars and bars).

    Example: For 15 bits and 1 2 6 1 pattern there are N=5 places (before/after the number and between 1s) to put K=2 zeros (number of leading zeros for a flush-right number). Number of ways is binomial(N + K – 1, K) i.e., binomial(5+2-1, 2) = 15.

    The key functions in the code below are next_combination_counts() and comb2number().

    Full program in C

    #include <assert.h>
    #include <stdbool.h>
    #include <stdio.h>
    
    #define SIZE(arr) (sizeof(arr)/sizeof(*(arr)))
    
    #define PRInumber "u"
    typedef unsigned number_t;
    
    // swap values pointed to by the pointer
    static void
    iter_swap(int* ia, int* ib) {
      int t = *ia;
      *ia = *ib;
      *ib = t;
    }
    
    // see boost::next_combinations_counts()
    // http://photon.poly.edu/~hbr/boost/combinations.html
    // http://photon.poly.edu/~hbr/boost/combination.hpp
    static bool 
    next_combination_counts(int* first, int* last) {
      /*
    0 0 2 
    0 1 1 
    0 2 0 
    1 0 1 
    1 1 0 
    2 0 0 
       */
        int* current = last;
        while (current != first && *(--current) == 0) {
        }
        if (current == first) {
            if (first != last && *first != 0)
                iter_swap(--last, first);
            return false;
        }
        --(*current);
        iter_swap(--last, current);
        ++(*(--current));
        return true;
    }
    
    // convert combination and pattern to corresponding number
    // example: comb=[2, 0, 0] pattern=[1,1] => num=5 (101 binary)
    static number_t 
    comb2number(int comb[], int comb_size, int pattern[], int pattern_size) {
      if (pattern_size == 0)
        return 0;
      assert(pattern_size > 0);
      assert(comb_size > pattern_size);
    
      // 111 -> 1000 - 1 -> 2**3 - 1 -> (1 << 3) - 1
      // 111 << 2 -> 11100
      number_t num = ((1 << pattern[pattern_size-1]) - 1) << comb[pattern_size];  
      int len = pattern[pattern_size-1] + comb[pattern_size];
      for (int i = pattern_size - 1; i--> 0; ) {
        num += ((1 << pattern[i]) - 1) << (comb[i+1] + 1 + len);
        len += pattern[i] + comb[i+1] + 1;
      }  
    
      return num;
    }
    
    // print binary representation of number
    static void 
    print_binary(number_t number) {
      if (number > 0) {
        print_binary(number >> 1);
        printf("%d", number & 1);
      }
    }
    
    // print array
    static void
    printa(int arr[], int size, const char* suffix) {
      printf("%s", "{");
      for (int i = 0; i < (size - 1); ++i)
        printf("%d, ", arr[i]);
      if (size > 0)
        printf("%d", arr[size - 1]);
      printf("}%s", suffix);
    }
    
    static void 
    fill0(int* first, int* last) {
      for ( ; first != last; ++first)
        *first = 0;
    }
    
    // generate {0,0,...,0,nzeros} combination
    static void
    init_comb(int comb[], int comb_size, int nzeros) {
      fill0(comb, comb + comb_size);
      comb[comb_size-1] = nzeros;
    }
    
    static int
    sum(int* first, int* last) {
      int s = 0;
      for ( ; first != last; ++first)
        s += *first;
      return s;
    }
    
    // calculated max width required to print number (in PRInumber format)
    static int 
    maxwidth(int comb[], int comb_size, int pattern[], int pattern_size) {
      int initial_comb[comb_size];
    
      int nzeros = sum(comb, comb + comb_size);
      init_comb(initial_comb, comb_size, nzeros);
      return snprintf(NULL, 0, "%" PRInumber, 
                      comb2number(initial_comb, comb_size, pattern, pattern_size)); 
    }
    
    static void 
    process(int comb[], int comb_size, int pattern[], int pattern_size) {
      // print combination and pattern
      printa(comb, comb_size, " ");
      printa(pattern, pattern_size, " ");
      // print corresponding number
      for (int i = 0; i < comb[0]; ++i)
        printf("%s", "0");
      number_t number = comb2number(comb, comb_size, pattern, pattern_size);
      print_binary(number);
      const int width = maxwidth(comb, comb_size, pattern, pattern_size);
      printf(" %*" PRInumber "\n", width, number);
    }
    
    // reverse the array
    static void 
    reverse(int a[], int n) {
      for (int i = 0, j = n - 1; i < j; ++i, --j) 
        iter_swap(a + i, a + j);  
    }
    
    // convert number to pattern
    // 101101111110100 -> 1, 2, 6, 1
    static int 
    number2pattern(number_t num, int pattern[], int nbits, int comb[]) {
      // SIZE(pattern) >= nbits
      // SIZE(comb) >= nbits + 1
      fill0(pattern, pattern + nbits);
      fill0(comb, comb + nbits + 1);
    
      int i = 0;
      int pos = 0;
      for (; i < nbits && num; ++i) {
        // skip trailing zeros
        for ( ; num && !(num & 1); num >>= 1, ++pos)
          ++comb[i];
        // count number of 1s
        for ( ; num & 1; num >>=1, ++pos) 
          ++pattern[i];
      }
      assert(i == nbits || pattern[i] == 0);  
      const int pattern_size = i;  
    
      // skip comb[0]
      for (int j = 1; j < pattern_size; ++j) --comb[j];
      comb[pattern_size] = nbits - pos;
    
      reverse(pattern, pattern_size);
      reverse(comb, pattern_size+1);
      return pattern_size;
    }
    
    int 
    main(void) {
      number_t num = 11769; 
      const int nbits = 15;
    
      // clear hi bits (required for `comb2number() != num` relation)
      if (nbits < 8*sizeof(number_t))
        num &=  ((number_t)1 << nbits) - 1;
      else
        assert(nbits == 8*sizeof(number_t));
    
      // `pattern` defines how 1s are distributed in the number
      int pattern[nbits];
      // `comb` defines how zeros are distributed 
      int comb[nbits+1];
      const int pattern_size = number2pattern(num, pattern, nbits, comb);
      const int comb_size = pattern_size + 1;
    
      // check consistency
      // . find number of leading zeros in a flush-right version
      int nzeros = nbits;
      for (int i = 0; i < (pattern_size - 1); ++i)
        nzeros -= pattern[i] + 1;
      assert(pattern_size > 0);
      nzeros -= pattern[pattern_size - 1];
      assert(nzeros>=0);
    
      // . the same but using combination
      int nzeros_comb = sum(comb, comb + comb_size);
      assert(nzeros_comb == nzeros);
    
      // enumerate all combinations 
      printf("Combination Pattern Binary Decimal\n");
      assert(comb2number(comb, comb_size, pattern, pattern_size) == num);
      process(comb, comb_size, pattern, pattern_size); // process `num`
    
      // . until flush-left number 
      while(next_combination_counts(comb, comb + comb_size))
        process(comb, comb_size, pattern, pattern_size);
    
      // . until `num` number is encounterd  
      while (comb2number(comb, comb_size, pattern, pattern_size) != num) {
        process(comb, comb_size, pattern, pattern_size);
        (void)next_combination_counts(comb, comb + comb_size);
      }
    
      return 0;
    }
    

    Output:

    Combination Pattern Binary Decimal
    {1, 0, 0, 1, 0} {1, 2, 6, 1} 010110111111001 11769
    {1, 0, 1, 0, 0} {1, 2, 6, 1} 010110011111101 11517
    {1, 1, 0, 0, 0} {1, 2, 6, 1} 010011011111101  9981
    {2, 0, 0, 0, 0} {1, 2, 6, 1} 001011011111101  5885
    {0, 0, 0, 0, 2} {1, 2, 6, 1} 101101111110100 23540
    {0, 0, 0, 1, 1} {1, 2, 6, 1} 101101111110010 23538
    {0, 0, 0, 2, 0} {1, 2, 6, 1} 101101111110001 23537
    {0, 0, 1, 0, 1} {1, 2, 6, 1} 101100111111010 23034
    {0, 0, 1, 1, 0} {1, 2, 6, 1} 101100111111001 23033
    {0, 0, 2, 0, 0} {1, 2, 6, 1} 101100011111101 22781
    {0, 1, 0, 0, 1} {1, 2, 6, 1} 100110111111010 19962
    {0, 1, 0, 1, 0} {1, 2, 6, 1} 100110111111001 19961
    {0, 1, 1, 0, 0} {1, 2, 6, 1} 100110011111101 19709
    {0, 2, 0, 0, 0} {1, 2, 6, 1} 100011011111101 18173
    {1, 0, 0, 0, 1} {1, 2, 6, 1} 010110111111010 11770
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms

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.