Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. //
  2. // Created by syh on 19-3-19.
  3. //
  4.  
  5.  
  6. /*
  7. * 与运算:1保留,0消除
  8. */
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. int exchangeOddEven(int x) {
  14. int odd = (x & 0xaaaaaaaa); // 1010 1010 1010 1010 1010 1010 1010 1010
  15. int even = (x & 0x55555555); // 0101 0101 0101 0101 0101 0101 0101 0101
  16. return (odd >> 1) + (even << 1);
  17. }
  18.  
  19. int main() {
  20. int x;
  21. cin >> x;
  22. cout << exchangeOddEven(x) << endl;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement