While talking about bit operation, programmers usually think about its efficiency. In embedded programming and system core optimization, appropriate use of bit operations is always a fascinating. When finding a job, it will help you a lot if you use bit operations when writing codes. Mastering simple bit arithmetic skills is necessary.
1. Get maximum int value
- int getMaxInt(){
- return (1 << 31) - 1;//2147483647,
- }
Another way:
- int getMaxInt(){
- return ~(1 << 31);//2147483647
- }
One more way:
- int getMaxInt(){
- return (1 << -1) - 1;//2147483647
- }
In C, if you don't know how many bytes an int is.
- int getMaxInt(){
- return ((unsigned int) - 1) >> 1;//2147483647
- }
2. Get minimum int value
- int getMinInt(){
- return 1 << 31;//-2147483648
- }
Another way
- int getMinInt(){
- return 1 << -1;//-2147483648
- }
3. Get maximum long value
C version:
- long getMaxLong(){
- return ((unsigned long) - 1) >> 1;//2147483647
- }
Java version :
- long getMaxLong(){
- return ((long)1 << 127) - 1;//9223372036854775807
- }
4. Multiply 2
- int mulTwo(int n){//计算n*2
- return n << 1;
- }
5. Divide by 2
- int divTwo(int n){
- return n >> 1;//除以2
- }
6. Multiple 2m
- int mulTwoPower(int n,int m){
- return n << m;
- }
7. Divide by 2m
- int divTwoPower(int n,int m){
- return n >> m;
- }
8. Check whether a number is odd or even
- boolean isOddNumber(int n){
- return (n & 1) == 1;
- }
9. Exchange values of two variables
- void swap(int *a,int *b){
- (*a) ^= (*b) ^= (*a) ^= (*b);
- }
Common version
- a ^= b;
- b ^= a;
- a ^= b;
10. Get absolute value
- int abs(int n){
- return (n ^ (n >> 31)) - (n >> 31);
- }
n>>31 will get the sign of n, if n is positive, then n>>31 is 0, if n is negative, n>>31 is -1. If n is positive, then n^0=n. If n is negative, then to calculate n^-1 , we need to calculate n and -1's complement and then do exclusive or operation. Then n will change the sign and the value is absolute value of n minus 1. So at last, do a minus -1 to get the absolute value.
11. Get bigger value of two values
- int max(int a,int b){
- return b & ((a-b) >> 31) | a & (~(a-b) >> 31);
- }
C version:
- int max(int x,int y){
- return x ^ ((x ^ y) & -(x < y));
- }
12. Get smaller value of two values
- int min(int a,int b){
- return a & ((a-b) >> 31) | b & (~(a-b) >> 31);
- }
C version:
- int min(int x,int y){
- return y ^ ((x ^ y) & -(x < y));
- }
13. Check whether two values have the same sign
- boolean isSameSign(int x, int y){
- return (x ^ y) >= 0;
- }
14. Calculate 2n
- int getFactorialofTwo(int n){//n > 0
- return 2 << (n-1);
- }
15. Check whether a value can be divided by 2
- boolean isFactorialofTwo(int n){
- return n > 0 ? (n & (n - 1)) == 0 : false;
- }
16. Get remainder of 2n
- int quyu(int m,int n){
- return m & (n - 1);
- }
17. Get average value of two values
- int getAverage(int x, int y){
- return (x + y) >> 1;
- ï½
Another way:
- int getAverage(int x, int y){
- return ((x ^ y) >> 1) + (x & y);
- }
18. Get the mth bit of a number n
- int getBit(int n, int m){
- return (n >> (m-1)) & 1;
- }
19. Set the bit to 1 at mth bit of a number n
- int setBitToOne(int n, int m){
- return n | (1 << (m-1));
- }
20. Set the bit to 0 at mth bit of a number n
- int setBitToZero(int n, int m){
- return n & ~(1 << (m-1));
- }
Here is one more article regarding using bit operation to realize addition, subtraction, multiplication and division. : Implementation of +,-,*,/ with bitwise operator
Great article! Very useful. The link at the end is dead due to an additional "article/" in the URL. Probably a typo! I was able to get to the page by removing it. Just though I'd post and let you know.