Bit operation skills

Source: Internet
Author: User

From http://blog.csdn.net/nash_/article/details/8262185

(FormatOmittedModified)

1. Get the maximum value of int type

 
Int getmaxint () {return (1 <31)-1; // 2147483647. Brackets cannot be omitted due to priority}

Another method:

 
Int getmaxint () {return-(1 <-1)-1; // 2147483647}

Another method:

Int getmaxint () {RETURN ~ (1 <31); // 2147483647}

Another method:When the C language does not know how many bytes int occupies

 
Int getmaxint () {return (unsigned INT)-1)/2; // 2147483647}

2. Get the minimum value of int type

 
Int getminint () {return 1 <31; //-2147483648}

Another method:

 
Int getminint () {return 1 <-1; //-2147483648}
3 . Obtain the maximum value of the long type.
 
Long getmaxlong () {return (unsigned long)-1)/2; // 2147483647}
Obtain the minimum value of long, which is the same as the maximum value of other types. 4 . Multiply by 2
 
Int multwo (int n) {// calculate n * 2 Return n <1 ;}
5 . Divide by 2
 
Int divtwo (int n) {// return n> 1 cannot be used for negative odd values; // divided by 2}

6. Multiply by 2 to the power of m

 
Int multwopower (int n, int m) {// calculate n * (2 ^ m) return n <m ;}
7 . Divide by 2 to the power of m
 
Int divtwopower (int n, int m) {// calculate n/(2 ^ m) return n> m ;}
8 . Judge the parity of a number
 
Boolean isoddnumber (int n) {return (N & 1) = 1 ;}
9 . You do not need to exchange temporary variables for two numbers)
 
Void swap (int * a, int * B) {(* A) ^ = (* B) ^ = (* A) ^ = (* B );}

General version:

 
A ^ = B; B ^ = A; A ^ = B;
10. Take the absolute value (On some machines,Efficiency ratio n> 0
? N:-n High)
 
Int ABS (int n) {return (N ^ (n> 31)-(N> 31);/* n> 31 get the N symbol, if n is positive, N> 31 is equal to 0. If n is negative, N> 31 is equal to-1. If n is positive, N ^ 0 = 0, the number remains unchanged, if n is a negative number and N ^-1 needs to calculate the complement code of N and-1, and then perform an exclusive or operation, the result N is changed to the absolute value of N minus 1, minus-1 is the absolute value */}

11. Take the maximum value of two numbers (On some machines,Efficiency Ratio A> B? A: B high)

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

C LanguageVersion:

 
Int max (int x, int y) {return x ^ (x ^ y) &-(x <y);/* If x <Y x returns 1, otherwise, 0 is returned, and 0 is executed and the calculation result is 0, which is the same as-1 and the calculation result */}

12. Take the minimum value of two numbers (On some machines,Efficiency Ratio A> B
? B: A high)

General edition:

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

C language:

Int min (int x, int y) {return y ^ (x ^ y) &-(x <y);/* If x <Y x returns 1, otherwise, 0 is returned, and 0 is executed and the calculation result is 0, which is the same as-1 and the calculation result */}

13. Determine if the symbols are the same

 
Boolean issamesign (int x, int y) {return (x ^ y)> 0; // true indicates that X and Y have the same symbol, and false indicates that X, Y has the opposite symbol. }

1 4. Calculate the N power of 2
Int getfactorialoftwo (int n) {// n> 0 return 2 <(n-1); // n of 2}
15. judge whether a number is a power of 2.
 
Boolean isfactorialoftwo (int n) {return (N & (n-1) = 0;/* if it is a power of 2, N must be 100... n-1 is 1111 .... therefore, the calculation result is 0 */}
16. Obtain the remainder from the nth power of 2
 
Int Quyu (int m, int N) {// n returns m to the power of 2 & (n-1);/* if it is a power of 2, N must be 100... n-1 is 1111 .... therefore, with the operation result, the non-0 bits of m in the N range are retained */}
17. Calculate the average of two integers
 
Int getaverage (int x, int y) {return (x + y)> 1 ;}

The following are the three most basic binary operations.18. From the low position to the high position, take the m position of N.

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

19. From the low position to the high position. Place N at the M position 1.

 
Int setbittoone (int n, int m) {return n |... 1... 000 N is performing an OR operation with this number */}

20. From the low position to the high position, set the m position of N to 0.

Int setbittozero (int n, int m) {return N &~ (1 <m-1);/* shift 1 to the left and find the M-bit. After the reverse operation, the value is 111... 0... 1111 N and then perform operations with this number */}
AppendixProgramThere is no substantial improvement in the efficiency of bit operations, and some are also the knowledge of bit operations (interview may encounter) Calculation Calculate n + 1
 
-~ N

CalculationN-1

 
~ -N

Reverse number
 
~ N +1;

If (x = A) x = B; If (x = B) x =;

 
X = a ^ B ^ X;

SIGN function. The parameter is N. If n> 0, 1 is returned. If n <0,-1 is returned. If n = 0, 0 is returned.

 
Return !! N-(unsigned) n> 31) <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.