蓝桥学院2019算法题1.6

 1 package bitOperation;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * @author zsh
 7  * @company wlgzs
 8  * @create 2019-02-14 16:18
 9  * @Describe 题5:将整数的奇偶位交换
10  */
11 public class Main5 {
12     public static void main(String[] args) {
13         Scanner scanner = new Scanner(System.in);
14         int n = scanner.nextInt();
15         //奇数位
16         int c = n & 0x55555555; //0101 0101 ...
17         //偶数位
18         int d = n & 0xaaaaaaaa; //1010 1010 ...
19         //奇数位左移1,偶数位右移1,然后异或
20         System.out.println((c<<1)^(d>>1));
21     }
22 }

 

posted @ 2019-02-14 16:36  梨花梦蝶  阅读(136)  评论(0编辑  收藏  举报