位运算的技巧

  1 package com.aixuexi.contact;
  2 
  3 public class Helloworld {
  4     public static void main(String[] args) {
  5         System.out.println("helloworld");
  6         System.out.println("helloworld");
  7         System.out.println("helloworld");
  8         System.out.println("helloworld");
  9         
 10         //位运算的技巧
 11 
 12         //1 、判断奇数
 13         System.out.println("=====判断奇数======");
 14         int n = 13;
 15         if (n % 2 == 1) { // 奇数
 16             System.out.println("是个奇数:" + n);    
 17         }
 18         if ((n & 1) == 1) {
 19             System.out.println("是个奇数:" + n);    
 20         }
 21         /*
 22          * 把n用二进制转换的话,只需要判断二级制的最后一位是0 or 1
 23          * 
 24          */
 25         
 26         //2、交换两个数
 27         
 28         //low
 29         System.out.println("====交换两个数=====");
 30         int x = 1, y =2;
 31         int temp = x; 
 32         x = y;
 33         y = temp;
 34         System.out.println(x);
 35         System.out.println(y);
 36         
 37         //高级方法
 38         System.out.println("====高级方法====");
 39         x = x ^ y;
 40         y = x ^ y;
 41         x = x ^ y;
 42         System.out.println(x);
 43         System.out.println(y);
 44         
 45         
 46         // 3、 找出没有重复的数
 47         System.out.println("==找出没有重复的数==");
 48         /*数组中只有一个数出现一次,剩余数都出现两次,找出出现一次的数*/
 49         int[] arr = new int[] {1,2,3,4,5,1,2,3,4};
 50         System.out.println(find(arr));
 51         /*
 52          *  就限于两个数相同 ^ 等于1 
 53          *  ^ 支持交换和结合 所以 1^2^3^4^5^1^2^3^4 = (1^1)^(2^2)^(3^3)^(4^4)^5 
 54         */
 55         
 56         // 4、m 的 n 次方 
 57         System.out.println("==m 的 n 次方 M1 ==");
 58         System.out.println(pow1(3));
 59         
 60         System.out.println("==m 的 n 次方 M2 ==");
 61         System.out.println(pow2(3));
 62         
 63         // 5、找出不大于N的最大的2的幂指数
 64         /*传统方法*/
 65         System.out.println("==找出不大于N的最大的2的幂指数 ==");
 66         System.out.println(findN(6));
 67         
 68         System.out.println(findN2(6));
 69     }
 70     
 71     
 72     
 73     //找出没有重复的数
 74     public static int find(int[] arr) {
 75         int tmp = arr[0];
 76         for (int i = 1; i < arr.length; i++) {
 77             tmp = tmp ^ arr[i];
 78         }
 79         return tmp;
 80     }
 81     
 82     //m 的 n 次方   M1 -- low
 83     public static int pow1(int n) {
 84         int tmp = 1;
 85         for(int i = 1; i <= n; i++) {
 86             tmp = tmp * 2; // m = 2
 87         }
 88         return tmp;
 89     }
 90     //m 的 n 次方   M2 
 91     public static int pow2(int n) {
 92         int sum = 1;
 93         int tmp = 2; // m = 2
 94         while(n != 0) {
 95             if((n & 1) == 1) {
 96                 sum *= tmp;
 97             }
 98             tmp *= tmp;
 99             n = n >> 1;
100         }
101         return sum;
102     }
103     //找出不大于N的最大的2的幂指数
104     public static int findN(int n) {
105         int sum = 1;
106         while(true) {
107             if(sum * 2 > n) {
108                 return sum;
109             }
110             sum *= 2;
111         }
112     }
113     
114     public static int findN2(int n) {
115         n |= n >> 1;
116         n |= n >> 2;
117         n |= n >> 4;
118         n |= n >> 8;
119         n |= n >> 16;
120         
121         return (n + 1) >> 1;
122     }
123 }
原文地址:https://www.cnblogs.com/yaozhenhua/p/11172027.html