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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:57:03+00:00 2026-06-18T02:57:03+00:00

Reading the book C – A reference manual (Fifth Edition) , I stumbled upon

  • 0

Reading the book “C – A reference manual (Fifth Edition)”, I stumbled upon this piece of code (each integer in the SET is represented by a bit position):

typedef unsigned int SET;

#define emptyset ((SET) 0)

#define first_set_of_n_elements(n) (SET)((1<<(n))-1)

/* next_set_of_n_elements(s): Given a set of n elements,
   produce a new set of n elements. If you start with the
   result of first_set_of_n_elements(k)/ and then at each
   step apply next_set_of_n_elements to the previous result,
   and keep going until a set is obtained containing m as a
   member, you will have obtained a set representing all
   possible ways of choosing k things from m things. */
SET next_set_of_n_elements(SET x) {
    /* This code exploits many unusual properties of unsigned arithmetic. As an illustration:
      if x == 001011001111000, then
      smallest       == 000000000001000
      ripple         == 001011010000000
      new_smallest   == 000000010000000
      ones           == 000000000000111
      returned value == 001011010000111
    The overall idea is that you find the rightmost
    contiguous group of 1-bits. Of that group, you slide the
    leftmost 1-bit to the left one place, and slide all the
    others back to the extreme right.
    (This code was adapted from HAKMEM.) */

    SET smallest, ripple, new_smallest, ones;
    if (x == emptyset) return x;
    smallest     = (x & -x);
    ripple       = x + smallest;
    new_smallest = (ripple & -ripple);
    ones         = ((new_smallest / smallest) >> 1) -1;
    return (ripple | ones);
}

I’m lost at the calculation of ‘ones’, and it’s significance in the calculation. Although I can understand the calculation mathematically, I cannot understand why this works, or how.

On a related note, the authors of the book claim that the calculation for first_set_of_n_elements “exploits the properties of unsigned subtractions”. How is (2^n)-1 an “exploit”?

  • 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-18T02:57:05+00:00Added an answer on June 18, 2026 at 2:57 am

    The smallest computation gets the first non-0 bit of your int. How does it works ?
    Let n be the bit length of your int. The opposite of a number x (bits bn-1…b0) is computed in a way that when you sum x to -x, you will get 2n. Since your integer is only n-bit long, the resulting bit is discarded and you obtain 0.
    Now, let b’n-1…b’0 be the binary representation of -x.
    Since x+(-x) must be equal to 2n, when you meet the first bit 1 of x (say at position i), the related -x bit will also be set to 1 and when adding the numbers, you’ll get a carry.
    To obtain the 2n, this carry must propagate through all the bits until the end of the bit sequence of your int. Thus, the bit of -x at each position j with i < j < n follows the properties below :

    bj + b’j + 1 = 10(binary)

    Then, from the above we can infer that :
    bj = NOT(b’j) and thus, that bj & b’j = 0

    On the other hand, the bits b’j of -x located at a position j such that 0 <= j < i are ruled by what follows :

    bj + b’j = 0 or 10

    Since all the related bj are set to 0, the only option is b’j = 0

    Thus, the only bit that is 1 in both x and -x is the one at position i

    In your example :

    x = 001011001111000
    -x = 110100110001000

    Thus,

    0.0.1.0.1.1.0.0.1.1.1.1.0.0.0
    1.1.0.1.0.0.1.1.0.0.0.1.0.0.0 AND
    \=====================/
    0.0.0.0.0.0.0.0.0.0.1.0.0.0

    The ripple then turns every contiguous “1” after position i (bit i included) to 0, and the first following 0 bit to 1 (due to the carry propagation). That’s why you ripple is :

    r(x) = 0.0.1.0.1.1.0.1.0.0.0.0.0.0.0

    Ones is computed as the division of smallest(r(x)) over smallest(x). Since smallest(x) is an integer with only a single bit set to 1 at position i, you have :

    (smallest(r(x)) / smallest(x)) >> 1 = smallest(r(x)) >>(i+1)

    The resulting integer has also only one bit set to 1, at say index p, thus, substract -1 to this value will get you an integer ones such that :

    For each j such that 0 <= j < p,
    onesj = 1
    For each j such that p <= j < n,
    onesj = 0

    Finally, the return value is the integer such that :

    • The first subsequence of 1-bit of the argument is set to 0.
    • All the 0-bit before the subsequence are set to 1.
    • The first 0-bit after the subsequence is set to 1.
    • The remaining bits are left unchanged

    Then, I can’t explain the remaining part since I did not understand the sentence :

    a set is obtained containing m as a member

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

Sidebar

Related Questions

I am reading some book and I have encountered a piece of code that
I am reading a book in which author used a code like this public
I'm trying to mastery php by reading book, and i'm stuck on this code
I was reading a book on templates and found the following piece of code:
Reading a book, and this code comes up: public class Test { private static
While reading a book about JavaScript I stumbled across an example: var names =
I'm reading the book 'Beginning F#', There's a short list for example code, to
I am reading this book to study the concepts of CUDA in depth. In
While reading a book, i came across this statement: The methods of an object
I'm reading book the C# programming Language, 4th Edition, by Anders Hejlsberg etc. There

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.