Organize notes -- bit operation

1. Get the maximum value of int type:

int getMaxInt(){  
        return (1 << 31) - 1;//2147483647, Brackets cannot be omitted due to priority  
}  

2. Get the maximum value of long type:

long getMaxLong(){  
    return ((long)1 << 127) - 1;//9223372036854775807  
}  

Others are similar.

3. Division 2 operation:

int divTwo(int n){//Negative odd operation not available  
    return n >> 1;//Divided by 2  
}  

4. m-power operation of division 2:

int divTwoPower(int n,int m){//Calculation n/(2^m)  
    return n >> m;  
}  

5. Judge the parity of a number

boolean isOddNumber(int n){  
    return (n & 1) == 1;  
}  

6. Exchange two numbers without temporary variables

a ^= b;  
b ^= a;  
a ^= b; 

7. Take absolute value

int abs(int n){  
return (n ^ (n >> 31)) - (n >> 31);  
/* n>>31 Obtain the sign of N, if n is a positive number, n > > 31 equals 0, if n is a negative number, n > > 31 equals - 1 
If n is a positive number, n^0=0, the number does not change. If n is a negative number, n ^ 1 needs to calculate the complement of N and - 1, and then perform exclusive or operation, 
Result n changes sign and is the absolute value of n minus 1, then minus - 1 is the absolute value */  
}  

8. Take the maximum of two numbers

int max(int a,int b){  
    return b & ((a-b) >> 31) | a & (~(a-b) >> 31);  
    /*If a > = B, (a-b) > > 31 is 0, otherwise - 1*/  
}  

9. Take the minimum of two numbers

int min(int a,int b){  
    return a & ((a-b) >> 31) | b & (~(a-b) >> 31);  
    /*If a > = B, (a-b) > > 31 is 0, otherwise - 1*/  
}  

10. Judge whether the symbols are the same

boolean isSameSign(int x, int y){ //Exception with 0  
    return (x ^ y) >= 0; // true Express x and y There are the same symbols, false Express x,y There are opposite signs.  
}  

11. n power of calculation 2

int getFactorialofTwo(int n){//n > 0  
    return 2 << (n-1);//2 Of n Secondary power  
}  

12. Judge whether a number is a power of 2

boolean isFactorialofTwo(int n){  
    return n > 0 ? (n & (n - 1)) == 0 : false;  
    /*If it's a power of two, n must be 100... n-1 is 1111 
       So the result of doing and operation is 0*/  
}  

13. n-th power of 2

int quyu(int m,int n){//n To the power of 2  
    return m & (n - 1);  
    /*If it's a power of two, n must be 100... n-1 is 1111 
     Therefore, the operation result should keep the non-zero bits of m in the n range*/  
}  

14. Average two integers

int getAverage(int x, int y){  
        return (x + y) >> 1;   
}  

15. From low position to high position, take n as the m position

int getBit(int n, int m){  
    return (n >> (m-1)) & 1;  
}  

16. From the low position to the high position, set the m position 1 of n

int setBitToOne(int n, int m){  
    return n | (1 << (m-1));  
    /*Move 1 left by m-1 to m, and get 000... 1... 000 
      n Doing or calculating with this number*/  
}  

17. From low position to high position, set the m position of n to 0

int setBitToZero(int n, int m){  
    return n & ~(1 << (m-1));  
    /* Move 1 to the left m-1 to find the m-th bit, and then reverse it to 111... 0... 1111 
       n And then do and operate on this number*/  
}  

Tags: Java

Posted on Sat, 04 Apr 2020 23:33:08 -0700 by fipp