Given an integer , print the next smallest and next largest number that have the same number of 1 bits in their binary representation
After Counting the number of 1’s in the number.How to determine the next smallest number?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
for next high you can use Hakmem 175 :
ITEM 175 (Gosper):
To get the next higher number with the same number of 1 bits:
For next lower I do not know a fast algorithm so I would use the classic approach, if the number is > then 2^0+2^1+…2^n then deduct one from your number and count the number of bits. The first number with n bits is the one.