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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:30:21+00:00 2026-05-25T09:30:21+00:00

Given a bitmask where the set bits describe where another number can be one

  • 0

Given a bitmask where the set bits describe where another number can be one or zero and the unset bits must be zero in that number. What’s a good way to iterate through all its possible values?

For example:

000 returns [000]
001 returns [000, 001]
010 returns [000, 010]
011 returns [000, 001, 010, 011]
100 returns [000, 100]
101 returns [000, 001, 100, 101]
110 returns [000, 010, 100, 110]
111 returns [000, 001, 010, 011, 100, 101, 110, 111]

The simplest way to do it would be to do it like this:

void f (int m) {
    int i;
    for (i = 0; i <= m; i++) {
        if (i == i & m)
            printf("%d\n", i);
    }
}

But this iterates through too many numbers. It should be on the order of 32 not 2**32.

  • 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-25T09:30:22+00:00Added an answer on May 25, 2026 at 9:30 am

    There’s a bit-twiddling trick for this (it’s described in detail in Knuth’s “The Art of Computer Programming” volume 4A §7.1.3; see p.150):

    Given a mask mask and the current combination bits, you can generate the next combination with

    bits = (bits - mask) & mask
    

    …start at 0 and keep going until you get back to 0. (Use an unsigned integer type for portability; this won’t work properly with signed integers on non-two’s-complement machines. An unsigned integer is a better choice for a value being treated as a set of bits anyway.)

    Example in C:

    #include <stdio.h>
    
    static void test(unsigned int mask)
    {
        unsigned int bits = 0;
    
        printf("Testing %u:", mask);
        do {
            printf(" %u", bits);
            bits = (bits - mask) & mask;
        } while (bits != 0);
        printf("\n");
    }
    
    int main(void)
    {
        unsigned int n;
    
        for (n = 0; n < 8; n++)
            test(n);
        return 0;
    }
    

    which gives:

    Testing 0: 0
    Testing 1: 0 1
    Testing 2: 0 2
    Testing 3: 0 1 2 3
    Testing 4: 0 4
    Testing 5: 0 1 4 5
    Testing 6: 0 2 4 6
    Testing 7: 0 1 2 3 4 5 6 7
    

    (…and I agree that the answer for 000 should be [000]!)

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

Sidebar

Related Questions

I am calculating the int equivalent of a given set of bits and storing
Given long long int x, y; , I want a function that can compare
I've got this small question - given a bitmask of weekdays (e.g., Sunday =
Given a Python object of any kind, is there an easy way to get
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given a (source) patch file, what's the easiest way to apply this patch on
Given a pointer to int , how can I obtain the actual int ?
Given a finite number of items which differ in kind, is it better to
Given: case class FirstCC { def name: String = ... // something that will
Given two DOM elements, say a and b, how can we determine which comes

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.