位运算实现x+1

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int main() {
 7     int x;
 8     while(cin >> x) {
 9         x = (x & 1) ? ( ( x & ((-1) << (((( ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) | ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) | (((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) > 0xF) << 2)) | ((((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) >> (((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) > 0xF) << 2)) > 0x3) << 1)) | ((((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) >> (((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) > 0xF) << 2)) >> ((((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) >> (((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) >> ((( ((x ^ -1) & ( - (x ^ -1) ) ) >> ( ( ((x ^ -1) & ( - (x ^ -1) ) ) > 0xFFFF) << 4) ) > 0xFF) << 3)) > 0xF) << 2)) > 0x3) << 1)) >> 1)))) | ((x ^ -1) & ( - (x ^ -1) ) )) : x | 1;
10         cout << x << endl;
11     }
12 }

 

posted @ 2015-09-30 16:36  悠悠我心。  阅读(252)  评论(0编辑  收藏  举报