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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:21:13+00:00 2026-06-18T08:21:13+00:00

My problem is as follows: I have a value x and a pattern p

  • 0

My problem is as follows: I have a value x and a pattern p both variables of the same size. The goal is to iterate through all bit patterns of x that are not masked by p.

Example:
if we have p = 1001, we want to find 0000, 0001, 1000, and 1001 – not necessarily in that order.

Standard implementation in C99 (the return value specifies whether we have returned all values already):

static bool next(size_t val, size_t mask, size_t *out) {
    if (val == mask) {
        return false;
    }
    size_t current = val & mask;
    size_t inc = 1;
    size_t new_val = current + inc;
    while ((new_val & mask) <= current) {
        inc++;
        new_val = current + inc;
    }
    *out = new_val;
    return true;
}

I would think there should be some trick to make this more efficient, but I can’t seem to find any big improvements (apart from computing the trailing zeros of the mask and setting the start value of inc appropriately, which isn’t much of an improvement).

Edit: Also important is the fact that for each generated value lots of additional work is generated, which means that lots of duplicates are out of the question (some duplicates, even if not recognizable would be ok, there aren’t any side effects to the work done, it’s just a slowdown).

  • 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-18T08:21:14+00:00Added an answer on June 18, 2026 at 8:21 am

    This generates all bit patterns in reverse order (initial value of val should be equal to mask):

    static bool next(size_t val, size_t mask, size_t *out) {
        if (val == 0) {
            return false;
        }
    
        *out = (val - 1) & mask;
        return true;
    }
    

    And this (slightly less obvious code) generates all bit patterns in direct order (initial value of val should be zero):

    static bool next(size_t val, size_t mask, size_t *out) {
        if (val == mask) {
            return false;
        }
    
        *out = (val - mask) & mask;
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is as follows: I have an environment with some variables defined like
Newbie here! My problem is as follows: I have a dynamically populating ul where
I'm facing a problem that I can summarize as it follows: I have a
My problem is as follows. I have an array of objects in the form
I have a mysql problem, my query looks as follows but not complete SELECT
Greetings, I have a problem as follows: I have an SQL variable declared: DECLARE
I have a problem as follows: I have few office locations and resources with
I have an optimization problem as follows. Given an array of positive integers, e.g.
My problem is as follows: I am working with MVVM pattern and I would
My problem is as follows: I have a bunch of Xpath queries, and I

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.