1. Gets the maximum value of type int:intGetmaxint () {return(1 << 31)-1;//2147483647, due to priority relationship, parentheses cannot be omitted}
2. Gets the maximum value of type long:LongGetmaxlong () {return((Long) 1 << 127)-1;//9223372036854775807}
3. In addition to the 2 operation:intDivtwo (intN) {//negative odd operations are not available return n >> 1;//divided by 2}
4. In addition to 2 m-square operations:intDivtwopower (intNintm) {//calculate n/(2^m) return n >> m;}
5. To determine the parity of a numberBooleanIsoddnumber (intN) {return(n & 1) = = 1; }
6. No temporary variable Exchange two number a^= b; b ^= A; A ^=b;
7. Take absolute ValueintAbsintN) {return(n ^ (n >>))-(n >> 31);/*n>>31 get n symbol, if n is positive, n>>31 equals 0, if n is negative, n>>31 equals-1 if n is positive n^0=0, number unchanged, if n is negative there is n^-1 need to calculate N and-1 of the complement, and then do the XOR operation, The result is the n variable and the absolute value of n minus 1, minus-1 is the absolute value .*/ }
8. Take a maximum of two numbersintMaxintAintb) {returnB & ((a) >> 31) | A & (~ (A-B) >> 31);/*if A>=b, (A-B) >>31 is 0, otherwise-1*/ }
9. Take the minimum value of two numbersintMinintAintb) {returnA & ((a) >> 31) | B & (~ (A-B) >> 31);/*if A>=b, (A-B) >>31 is 0, otherwise-1*/ }
10. Determine if the symbols are the sameBooleanIssamesign (intXintY) {//There are 0 cases of exception return (x ^ y) >= 0;//true indicates that X and Y have the same symbol, and false indicates the opposite sign and X, Y. }
11. Calculates the n-th-square of 2intGetfactorialoftwo (intN) {//n > 0 return 2 << (n-1);//2 of the N-th party}
12determine if a number is a power of 2.BooleanIsfactorialoftwo (intN) {returnn > 0? (N & (n-1)) = = 0:false;/*if it is a power of 2, n must be ... n-1 is 1111 .... So the result of doing and calculating is 0*/ }
13. to 2 of the n-th-square to take the remainderintQuyu (intMintN) {//N is 2 of the second return M & (n-1);/* If it is a power of 2, n must be ... n-1 is 1111 .... therefore do with the result of the operation retain m in the N range of the non-0 bits */}
14. Take an average of two integersintGetaverage (intXintY) {return(x + y) >> 1;}
15. From low to High, take N to bits mintGetbit (intNintm) {return(n >> (m-1)) & 1; }
16. From low to high, place n at position 1intSetbittoone (intNintm) {returnn | (1 << (m-1));/*move 1 to the left m-1 bit to find the first m bit, get 000 ... 1...000 N in and this number do or operate*/ }
17. From low to high, place n at position 0intSetbittozero (intNintm) {returnN & ~ (1 << (m-1));/*Move the 1 left m-1 bit to find the first m bit, then turn back to 111 ... 0...1111 N again and this number do with arithmetic*/ }
java--bit arithmetic