Summary of common operations for bitwise operations

Source: Internet
Author: User
Tags bitwise

Bit operation is a magical operation, can be very clever to solve some problems, at the same time the speed is very fast. The basic concepts of bit operations, as well as some common methods for bitwise operations, are described below. 1, the basic concept of bitwise operation

Pictures from Google.

It should be noted here that the bitwise operations are all converted into binary, generally 32-bit length. 2, the commonly used bit operation method

2.1 Exchange two numbers

                       int x = 1, y = 2;
                        x ^= y;
                        y ^= x;
                        x ^= y;

2.2 To determine whether a number is a power of 2
To determine whether a number is 2 power, to analyze the number of the second power of the binary representation, there must be only one 1, the rest is 0, to remember this property. and n& (n-1) is also an important property that can be achieved by turning the rightmost 1 of n into 0.

public static Boolean Mici (int a) {
        if (a==0) return false;
        Return (a& (A-1)) ==0;
    }

2.3 To find the absolute value of a number
For negative numbers, you can get positive numbers by adding them back. First is to move 31 digits to the right, if a positive number will get 0, if the negative is to get 1, for positive numbers can be directly output, for negative numbers can be reversed plus 1.

                                int i=-1;
                                int j=i>>31;
                                Return  (i^j)-j;

2.4 To find the opposite number of a number
You can use the reverse operation, but note that you need to add 1 after the counter.

                                int a=-1;
                                int b=~a+1;

2.5 to judge the parity of a number
It only needs to be done with 1, because the number of 1 in an odd binary representation is definitely even, and the number of 1 in the binary representation of an even number is definitely odd.

                                int a=9;
                                Return (a&1) ==1;

2.6. Realize addition Operation
XOR or operation can achieve the addition of no carry, in order to realize the carry operation, it is necessary to have a carrying variable record whether the rounding.

public static int Getsum (int a, int b) {while
        (b!= 0) {
            int = a ^ b;//No carry addition
            B = (A & B) << 1 ;
            A = C;
        }
        return A;
    }

2.7 Implementation Subtraction

    Implementation subtraction A-b public
    static int Jianfa (int a,int b) {return
        getsum (A, ~b+1);
    

2.7, realize the multiplication of positive numbers

The principle is still calculated by addition. Add B A.

public static int Multi (int a, int b) {
        int ans = 0;
        while (b > 0) {
            if ((b & 1)!= 0) {
                ans = getsum (ans, a);//two numbers add
            }
            a = a << 1;
            b = b >> 1;
        }
        return ans;
    }

2.8, the realization of positive Division
In essence, the subtraction operation is performed to see how many times the divisor can be subtracted from the dividend.

    Implement positive division public
    static int  divide (int a,int b) {
        int res=0;
        while (a>=b) {
            a=jianfa (a,b);
            res++;
        }
        return res;
    }

2.9, statistics a number of the binary representation of how many 1
The use of the property is a number and this number minus 1 phase of the implementation of the effect is to the binary representation of the last 1 into 0. So you can count the number of times, that is, the number of 1.

public int hammingweight (int n) {
        int sum=0;
        while (n!=0) {
            sum++;
            n=n& (n-1);
        }
        return sum;
    }

2.10, find the number of occurrences in the sequence only once, the remaining number appears 2 times
A number and own XOR or operation is 0, so you can find out, need to know the nature is a^b^c=a^c^b

public int singlenumber (int[] nums) {
        int r=0;
       for (int i=0;i<nums.length;i++) {
           r=r^nums[i];
       }
        return r;
    }

2.11, Reverse binary sequence, and then output integer
The original number is shifted to the right one bit, then the lowest bit is assigned to the new number, and the new number shifts to the left.

private static int rever (int a) {
        int res=0;
        for (int i = 0; i < i++) {
            res=res<<1| ( A&1);
            a>>=1;
        }
        return res;
    }

2.12. Achieve the average of two numbers

 int a=1;
                                    int b=9;
Return (A+B) >>1; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.