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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:18:07+00:00 2026-05-14T23:18:07+00:00

I am trying to make a loop that loops through all different integers where

  • 0

I am trying to make a loop that loops through all different integers where exactly 10 of the last 40 bits are set high, the rest set low. The reason is that I have a map with 40 different values, and I want to sum all different ways ten of these values can be multiplied. (This is just out of curiosity, so it’s really the “bitmanip”-loop that is of interest, not the sum as such.)

If I were to do this with e.g. 2 out of 4 bits, it would be easy to set all manually,

0011 = 3,
0101 = 5,
1001 = 9,
0110 = 6,
1010 = 10,
1100 = 12,

but with 10 out of 40 I can’t seem to find a method to generate these effectively. I tried, starting with 1023 ( = 1111111111 in binary), finding a good way to manipulate this, but with no success. I’ve been trying to do this in C++, but it’s really the general method(if any) that is of interest. I did some googling, but with little success, if anyone has a good link, that would of course be appreciated too. 🙂

  • 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-14T23:18:08+00:00Added an answer on May 14, 2026 at 11:18 pm

    A bit more complicated, but done purely by bit manipulation. Your example:

    #define WIDTH 4
    #define BITS 2
    
    void printbits(long pattern) {
      long bit;
      for (bit = 1L << WIDTH - 1; bit; bit >>= 1)
        putchar(pattern & bit ? 49 : 48);
      putchar('\n');
    }
    
    void movebits(pattern, bit) {
      long mask = 3L << bit;
      while (((pattern ^= mask) & mask) && (mask < 1L << WIDTH)) {
        mask <<= 1;
        printbits(pattern);
        if (bit)
          movebits(pattern, bit - 1);
      }
    }
    
    int main() {
      long pattern = (1L << BITS) - 1L, mask;
      printbits(pattern);
      movebits(pattern, BITS - 1);
    }
    

    Your real application:

    #define WIDTH 40
    #define BITS 10
    

    and, as polygenelubricants says, be prepared to wait for bit 🙂 Of course, you will replace printbits with something more useful to you…

    (Edited for insufficient testing :/ Damn typos…)

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

Sidebar

Related Questions

Simple Substitution Cipher. I'm trying to make a looping construct that would loop through
I'm trying to make a loop so that a user can enter a number,
I am trying to loop through all files and folders and perform an action
During program optimization, trying to optimize a loop that iterates through a vector, I
I will keep this very short: I'm trying to make a loop through an
After trying to make a while(bool) loop and it failing because I couldn't see
Im trying to make a basic while loop example im preperation for my next
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Hey all, my first post :D Problem: I'm trying to make a template gallery,

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.