20 bits of arithmetic that good programmers must know

  • 2020-04-02 01:33:31
  • OfStack

Carry a bit operations, people tend to think of its high efficiency, the core of both embedded programming and optimization system code, the use of appropriate bit operations is always an attractive means, or when you to apply for a job in the code written to the appropriate bit operations will also allows you to add a bright spot, initially when I read "the beauty of the programming for the number of" 1 ", I began to feel bit operations is so beautiful, then read the Hacker 's Delight ", says Henry s. arren apply bit operations so elusive, many programs are very subtle, I think people who use a lot of this code in a normal program are crazy! However, it is still necessary to master simple bit operation skills, so I am writing this article today to share some of my accumulated bit operation skills with you, these skills will not be such as the number of "1" skills, is the most basic one-line bit operation skills!

One. Get an int maximum


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

Another way to write it

int getMaxInt(){  
    return -(1<<-1) - 1;//2147483647  
}  

Another way to write it

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

You don't know how many bytes an int takes in C

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

Get an int minimum

int getMinInt(){  
    return 1<<31;//-2147483648  
 }  

Another way to write it

int getMinInt(){  
    return 1 << -1;//-2147483648  
}  

Get the maximum value of type long

C language version


long getMaxLong(){  
    return ((unsigned long)-1) >> 1;//2147483647  
}  

JAVA version

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

Get the minimum of long, the same as the maximum, the minimum of any other type.

Four times two


int mulTwo(int n){//Calculate n * 2 & have spent & have spent
    return n<<1;  
}

Five divided by two

int divTwo(int n){//The operation of negative odd Numbers is not available & NBSP;
    return n>>1;//Divided by 2 & have spent
}

Six times two to the m

int divTwoPower(int n,int m){//Calculate n/(2 ^ m) & have spent
    return n>>m;  
}  

Seven over two to the m
The same code at the page code block index 10
Eight. Judge the parity of a number

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

9. Do not exchange two Numbers of temporary variables (common test in interview)

C language version


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

General edition (written separately in some languages)

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

Take the absolute value (on some machines, the efficiency ratio n > 0   ?   N high n: -)

int abs(int n){  
return (n ^ (n >> 31)) - (n >> 31);  
  
}

Eleven. Take the maximum of two Numbers (on some machines, the efficiency ratio of a > B? A: b)

General version


int max(int a,int b){  
    return b&((a-b)>>31) | a&(~(a-b)>>31);  
      
}  

C language version

int max(int x,int y){  
    return x ^ ((x ^ y) & -(x < y));  
      
}  

Twelve. Take the minimum of two Numbers (on some machines, the efficiency ratio of a > B? B: a high)

General version


int min(int a,int b){  
    return a&((a-b)>>31) | b&(~(a-b)>>31);  
      
} 

C language version

int min(int x,int y){  
    return y ^ ((x ^ y) & -(x < y));  
           
}  

Thirteen. Determine whether the signs are the same

boolean isSameSign(int x, int y){  
    return (x ^ y) > 0; //True means that x and y have the same sign, false means that x and y have opposite signs. & have spent
}  

Fourteen. Compute 2 to the n

int getFactorialofTwo(int n){//n > 0  
    return 2<<(n-1);//Two to the n & PI;
}

Fifteen. Determine whether a number is a power of two

boolean isFactorialofTwo(int n){  
    return (n & (n - 1)) == 0;  
      
}  

Sixteen. Mod two to the n

int quyu(int m,int n){//N is 2 PI & PI;
    return m & (n - 1);  
      
}  

Seventeen. Take the average of two integers

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

Another way to write it

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

}   

Here are three of the most basic operations on bits

18. From low to high, take the MTH bit of n


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

Nineteen. From low to high. Take the MTH position of n, 1

int setBitToOne(int n, int m){  
    return n | (1<<(m-1));  
      
}

Twenty. From low to high, take the MTH position of n, 0

int setBitToZero(int n, int m){  
    return n & ~(1<<(m-1));  
      
}  

In addition, there are some bit-counting techniques that do not substantially improve the efficiency of the program, and some common sense of bit-counting.
Calculate the n + 1

-~n  

Calculation of n - 1

~-n  

Take the opposite

~n + 1; 

Another way to write it

(n ^ -1) + 1;  

If (x == a) x = b; If (x == b) x = a;

x = a ^ b ^ x;

Sign function, which takes argument n, when n > 0 returns 1, n < 0 returns negative 1, and n equals 0 returns 0

return !!n - (((unsigned)n>>31)<<1);   


Related articles: