# lowbit

N反码则是！！！（表示？的取反）01111
N补码也就是-N！！！10000
N&（-N）为00010000，求得最后的10串。

This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr’s existence know very little about her. For example, no one has any clue as to which time-period she is originally from.

But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - “Dokhina Batash”. She was making the final adjustments for her first trip when she noticed that a vital program was not working properly. The program was supposed to take a number N and find what Zephyr called its Onoroy value.

The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly and was on her way.

You are now given a similar task. Find the first number after N which has the same Onoroy value as N.

Input：
Input starts with an integer T (≤ 65), denoting the number of test cases.

Each case begins with an integer N (1 ≤ N ≤ 109).

Output：
For each case of input you have to print the case number and the desired result.

Sample input：

5
23
14232
391
7
8

Sample output:

Case 1: 27
Case 2: 14241
Case 3: 395
Case 4: 11
Case 5: 16

https://blog.csdn.net/chen_logos/article/details/45869997参考本篇文章。

N=011110
x=N&(-N)=10(第一个10串，虽然没有0)
y=N+x=100000(让后面连续的1进位改为0，前面的01变10)
z=N&(~y)=011110=11110（会留下第一个连续1的片段），这一步理解了你就超神了（这一步我搞了了两小时才彻底弄清楚TwT）。首先明确这一步的目的是，找第一个连续1片段，去掉一个1，再从右到左放入y中。连续的1片段在N到y的过程中参与了进位，而其他无关位置的10是不会改变的，所以我们的目标是通过位运算让进位的部分保留，其他位置去掉，先 ~y取反再&N可以让改变的东西变回去，没变的东西就不要了，（读者可以自己举例子验证一下）。


//
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
int T;
cin>>T;
int TT=T;
while(T--)
{
int n,x,y,z;
int res,ans;
cin>>n;
x=n&(-n);//lowbit取最后的10串
y=n+x;//将最后一个连续的1串进位改为10串，如1111->10000
z=n&(~y);//第一个连续1串，原来变化的部分不变，不变的变=.=
res=(z/x)>>1;//最右边的10串去掉，要右移一位，去掉进位的1
ans=y|res;//连续1段和前部分合并
printf("Case %d: %d\n",TT-T,ans);
}
}



04-11 1万+

09-06 3421
04-07 113
08-12 499
11-22 446
07-04 2195
12-26 45
08-10 257
02-28
09-28
09-18
12-20