c - Finding next bigger number with same number of set bits -


I am working on a problem where I have been given a number, let me get the next big element to meet with When searching on the Internet on the number of sets of bits, I found an interesting piece of code, which bit (bit magic) in the code:

  unsigned nexthi_same_count_ones (unsigned one) {/ * Works for the length of any word * / unsigned C = (A and A); Unsigned R = A + C; Return (((R ^ A)> 2) / C). R); }  

But I want to understand the underlying logic of the algorithm that it will always work, matters of all limits will be handled properly.

"post-text" itemprop = "text"

In the next higher number, 1 1 on the left side of 0 while the remaining 1 s moves in the right direction

< Ul>
  • Adds the code to 1 ,
  • to a (from which the next higher 0 , Is turning on all those bits)
  • Pre- or at least important runs are scored, a situation is extended to the left
  • to move it right From the two conditions, the original left moves to the right of a place (except for any one 0 high position),
  • a The more the 0 is at the right edge of the lower, the less divides by 1 .

  • Comments