Excellent programmer must know 20 bit arithmetic skills _c language

Source: Internet
Author: User
Tags arithmetic

A lifting bit operation, people often think of its efficiency, whether embedded programming or optimize the system's core code, the proper use of bitwise operations is always a fascinating means, or when you are looking for a job, write the appropriate bit in the code will also allow your program to add a glimmer of light, initially when I read "The beauty of programming" to ask "1 of the number "When, I began to think that the bit arithmetic is so beautiful, later read" Hacker's Delight ", regrets to Henry S.warren put the use of the position of so shadowy, many procedures are very subtle, I think in a common program in a large number of people who use such code is crazy! But mastering the simple bit arithmetic skill is still necessary, so write this article today to share some of the bit-computing skills that I've accumulated, and these techniques are not like "1 number" techniques, but are the most basic line-and-digit arithmetic skills!

I. Get the INT type max value

Copy Code code as follows:

int Getmaxint () {
Return (1<<31)-1;//2147483647, parentheses cannot be omitted due to precedence relationships
}

Another way of writing
Copy Code code as follows:

int Getmaxint () {
Return-(1<<-1)-1;//2147483647
}

Another way of writing
Copy Code code as follows:

int Getmaxint () {
return ~ (1<<31);//2147483647
}

C language does not know when an int occupies several bytes
Copy Code code as follows:

int Getmaxint () {
return ((unsigned int)-1) >> 1;//2147483647
}

two. Get the int type minimum value
Copy Code code as follows:

int Getminint () {
Return 1<<31;//-2147483648
}

Another way of writing
Copy Code code as follows:

int Getminint () {
Return 1 << -1;//-2147483648
}

three. Get the maximum of a long type

C language version

Copy Code code as follows:

Long Getmaxlong () {
Return ((unsigned long)-1 >> 1;//2147483647
}

Java version
Copy Code code as follows:

Long Getmaxlong () {
Return ((long) 1<<127) -1;//9223372036854775807
}

Get the long minimum, and the maximum value of the other type, the minimum value is the same.

Four. Multiply 2 operations

Copy Code code as follows:

int multwo (int n) {//Compute n*2
Return n<<1;
}

Five. Divided by 2 operation
Copy Code code as follows:

int divtwo (int n) {//negative odd operation not available
Return n>>1;//divided by 2
}

six. Times 2 m-squared
Copy Code code as follows:

int divtwopower (int n,int m) {//Calculation n/(2^m)
Return n>>m;
}

Seven. Divided by 2 m-second party
Copy Code code as follows:

int divtwopower (int n,int m) {//Calculation n/(2^m)
Return n>>m;
}

eight. Judge the parity of a number
Copy Code code as follows:

Boolean isoddnumber (int n) {
Return (n & 1) = = 1;
}

Nine. Exchange two numbers without a temporary variable (interview often)

C language version

Copy Code code as follows:

void swap (int *a,int *b) {
(*a) ^= (*b) ^= (*a) ^= (*b);
}

General edition (scoring in some languages)
Copy Code code as follows:

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

10. Absolute value (on some machines, more efficient than n>0?) N:-n High)
Copy Code code as follows:

int abs (int n) {
Return (n ^ (n >>))-(n >> 31);
/* n>>31 Gets the symbol of n, if n is positive, n>>31 equals 0, if n is negative, n>>31 equals 1.
If n is positive n^0=0, the number is constant, if n is negative there is n^-1 need to compute the complement of N and-1, then do the XOR or operation,
The result is n and 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 is higher than a>b a:b)

General Edition

Copy Code code as follows:

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*/
}

C language version
Copy Code code as follows:

int max (int x,int y) {
return x ^ ((x ^ y) &-(x < y));
/* If X<y x<y returns 1, otherwise returns 0,
, with 0 do with the result of the operation 0, with-1 do with the result of the operation not to change * *
}

12. Take the minimum value of two numbers (on some machines, efficiency is higher than a>b b:a)

General Edition

Copy Code code as follows:

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*/
}

C language version
Copy Code code as follows:

int min (int x,int y) {
Return y ^ ((x ^ y) &-(x < y));
/* If X<y x<y returns 1, otherwise returns 0,
With 0 to do with the result of the operation 0, with-1 do with the result of the operation not to change * *
}

13. Determine if the symbol is the same
Copy Code code as follows:

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.
}

14. Calculate 2 of the N-second side
Copy Code code as follows:

int getfactorialoftwo (int n) {//n > 0
Return 2<< (n-1);//2 N-Time Square
}

15. Determine whether a number is a power of 2
Copy Code code as follows:

Boolean isfactorialoftwo (int n) {
Return (N & (n-1)) = = 0;
/* If is the power of 2, n must be ... n-1 is 1111 ....
So do with the result of the operation for 0*/
}

16. To 2 of the N-second side of the remainder
Copy Code code as follows:

int Quyu (int m,int n) {//n is 2 of the second party
Return M & (n-1);
/* If is the power of 2, n must be ... n-1 is 1111 ....
So do with the result of the operation keep m in the N range of non 0 bits * *
}

17. Calculate the average of two integers
Copy Code code as follows:

int getaverage (int x, int y) {
Return (x+y) >> 1;


Another way of writing
Copy Code code as follows:

int getaverage (int x, int y) {
Return ((x^y) >> 1) + (x&y);
/* (x^y) >> 1 gets x,y One of the 1 bits and divided by 2,
X&y get X,y are 1 of the parts, plus together is the average of the * *

}

Here are the three most basic operations for BITS

18. From low to High, take N of the first m bit

Copy Code code as follows:

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

19. From low to high. Place m position 1 of N
Copy Code code as follows:

int setbittoone (int n, int m) {
return n | (1<< (m-1));
/* Move 1 left m-1 bit to find the first m bit, get 000 ... 1...000
N in and this number do or operation * *
}

20. From low to high, place N of the m position 0
Copy Code code as follows:

int Setbittozero (int n, int m) {
return N & ~ (1<< (m-1));
/* will be 1 left m-1 bit to find the first m, after the reverse into 111 ... 0...1111
n again and this number do with the operation * *
}

There are some bit algorithms that have no real improvement in the efficiency of the program, some of which are also common sense (interview may be encountered)
Calculate n+1
Copy Code code as follows:

-~n

Calculate n-1
Copy Code code as follows:

~-n

Take the opposite number
Copy Code code as follows:

~n + 1;

Another way of writing
Copy Code code as follows:

(n ^-1) + 1;

if (x = = a) x = b; if (x = = b) x = A;
Copy Code code as follows:

x = a ^ b ^ x;

Sign function, the parameter is N, return 0 when N>0 returns 1,N&LT;0 when returning to -1,n=0
Copy Code code as follows:

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.