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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:59:52+00:00 2026-05-27T04:59:52+00:00

I forgot a bit hack to generate all integers with a given number of

  • 0

I forgot a bit hack to generate all integers with a given number of 1s. Does anybody remember it (and probably can explain it also)?

  • 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-27T04:59:53+00:00Added an answer on May 27, 2026 at 4:59 am

    From Bit Twiddling Hacks

    Update Test program Live On Coliru

    #include <utility>
    #include <iostream>
    #include <bitset>
    
    using I = uint8_t;
    
    auto dump(I v) { return std::bitset<sizeof(I) * __CHAR_BIT__>(v); }
    
    I bit_twiddle_permute(I v) {
        I t = v | (v - 1); // t gets v's least significant 0 bits set to 1
        // Next set to 1 the most significant bit to change, 
        // set to 0 the least significant ones, and add the necessary 1 bits.
        I w = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(v) + 1));  
        return w;
    }
    
    int main() {
        I p = 0b001001;
        std::cout << dump(p) << "\n";
        for (I n = bit_twiddle_permute(p); n>p; p = n, n = bit_twiddle_permute(p)) {
            std::cout << dump(n) << "\n";
        }
    }
    

    Prints

    00001001
    00001010
    00001100
    00010001
    00010010
    00010100
    00011000
    00100001
    00100010
    00100100
    00101000
    00110000
    01000001
    01000010
    01000100
    01001000
    01010000
    01100000
    10000001
    10000010
    10000100
    10001000
    10010000
    10100000
    11000000
    

    Compute the lexicographically next bit permutation

    Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the bit pattern is 00010011, the next patterns would be 00010101, 00010110, 00011001,00011010, 00011100, 00100011, and so forth. The following is a fast way to compute the next permutation.

    unsigned int v; // current permutation of bits 
    unsigned int w; // next permutation of bits
    
    unsigned int t = v | (v - 1); // t gets v's least significant 0 bits set to 1
    // Next set to 1 the most significant bit to change, 
    // set to 0 the least significant ones, and add the necessary 1 bits.
    w = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(v) + 1));  
    

    The __builtin_ctz(v) GNU C compiler intrinsic for x86 CPUs returns the number of trailing zeros. If you are using Microsoft compilers for x86, the intrinsic is _BitScanForward. These both emit a bsf instruction, but equivalents may be available for other architectures. If not, then consider using one of the methods for counting the consecutive zero bits mentioned earlier.

    Here is another version that tends to be slower because of its division operator, but it
    does not require counting the trailing zeros.

    unsigned int t = (v | (v - 1)) + 1;  
    w = t | ((((t & -t) / (v & -v)) >> 1) - 1);  
    

    Thanks to Dario Sneidermanis of Argentina, who provided this on November 28, 2009.

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

Sidebar

Related Questions

We all know that deadlines and/or critical bugfixes and make us forget a bit
I just forgot the password. Can anyone help me how to get back the
SORRY:: I forgot that params[:user_id] does not exist in the controller, using current_user.id !
EDIT Sorry I forgot the most important part here. Each key can have more
So I did a bit of C programming a while ago and basically forgot
Duplicate: Best algorithm to count the number of set bits in a 32-bit integer?
I forgot the jQuery command that will clear all list elements from a list.
I've got a copy of Matlab from 2004 (forgot what the version number would
I have a file that I should have committed but forgot all about. I
Okay, this may be a bit basic. The HTML is : <div class=parents-of-all> <p>

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.