Find next number with specific hamming weight
问题 Given a certain integer x , I wish to compute the next higher integer y which has a certain hamming weight w . Mind that the hamming weight of x does not have to be w as well. So, for example x = 10 (1010) and w = 4 the result should be y = 15 (1111). Obviously, I could achieve this by just incrementing x but that would be a very slow solution for high numbers. Can I achieve this by the means of bit shifts somehow? 回答1: There are three cases: the Hamming weight (a.k.a. bitwise population