Examples of bit operation in Java

Follow the instructions bits, are:

<<: Left, right up most 0 ==> original number by 2 ^ n

>>: the right, the leftmost symbol bits fill ==> original number by 2 ^ n

>>>: unsigned right, do fill the left 0

&: Bitwise AND

|: Bitwise or

~: Bitwise

^: Bitwise XOR

Simple basic concept described above to see example (* ^ ▽ ^ *)

1, and the sum of two numbers (bit operation)

When the bit operation, the same as if the corresponding bit (1), after the phase is shifted left one bit, corresponding to two multiplied by one into the original data twice, i.e. two numbers. If the binary bits corresponding to different, corresponding bit exclusive OR, addition with the foregoing.

public static  int sum(int n, int m) {
        return  ((n&m)>>1) + n^m;
 }

2, an average of two numbers (bit operation)

The same part is divided by two plus the sum divided by two different parts

 public static  int aver(int n, int m) {
        return  (n&m) + (n^m)>>1;
  }

 

3, to write a binary function returns a number, such as 4 15000011111 return.

We can follow the traditional method, converted into binary. Kept modulo two addition, there are several look remainder 1.

 public static int  PrintOneNum(int n) {
        int count = 0;
        while(n != 0) {
            if(n%2 == 1) {
                count++;
            }
            n /= 2;
        }
        return count;
    }

Another way that we bitwise operations, will do so

With 00000111 (7) to give chestnut: 000001117
                                                  &. 6 0000 0110
                                                      0000 0110. 6
                                                  &. 5 0000 0101
                                                       0000 0100. 4
                                                 &. 3 0000 0011
                                                        0000 0000 0

I found: one phase and the next, until the result is zero. The number of binary phase is the original number of 1s. Implemented by the following code:

 public static int PrintOneNum(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n - 1);
        }
        return count;
    }
 

4, to get all the even bit and an odd bit binary number in the sequence, each binary sequence output.

To determine the number on each bit is 0 or 1, the easiest way is to shift operation of & 1.

 public static void printNum(int n) {
        System.out.print("偶数位;");
        for (int i = 31; i >= 1 ; i-=2) {
            System.out.print(((n>>i)&1) + " ");
        }
        System.out.println();
        System.out.println("=========");
        System.out.print("奇数位;");
        for (int i = 30; i >= 0 ; i-=2) {
            System.out.print(((n>>i)&1) + " ");
        }
    }

To be continued ... hahahaha
                                                    

 

Published 51 original articles · won praise 14 · views 2336

Guess you like

Origin blog.csdn.net/qq_41185460/article/details/102616942