Simple bit operations and _ conditional compilation fairly easy to use _

         Rattling, there are so many single-chip registers, nearly every binary bits are in control of a power supply system hardware modules, its operation if a little careless, they would cut off the energy supply of a module, so we operate its register the time and had to talk about some of the commonly used simple bit computing you, ooh ooh,

         1: Let us talk about the text as it shaped shift operation it "" "and" "" He is a binary operator, X "n. The X bit is a binary manner, n mobile units left in the ICC compiler environment, we often use an operator is:

PORTA | = BIT (n), in fact, his essence is PORTA | = (1 "n), you just open <macros.h> will find the mystery ...

         Also, if we assume that X = 1; that X "0 = 1;

                                           X《1=2;

                                           X《2=4;

                                           X《3=8;

                                           ......... you find it? He left an equivalent multiplied by 2.

                 If we assume that X = 8; that X "0 = 8;

                                           X》1=4;

                                           X》2=2;

                                           X》3=1;

                                           ......... you find it? Right one equivalent divided by two.

         Calculation instead of the division by shift operation program, greatly improve the efficiency of the code ...

Left and right not only to do with the division, but also from a long list of data, filtering out the data we want.

               If we want to extract data unsigned int X 8-11 is in, save to the MAX

                        MAX=((X《4)》12);

         2: the negation operator "~", the right and wrong culprit, the 1 to 0, the 0 to 1;

         3: AND operation, "&", so you can imagine, and it is dictatorship, has a veto, as long as there is a 0 0

         4: OR "|", rattling, or a good man, one vote can pass oh ~ ~ ~

        5: XOR operation "^", remember word like, seek common ground, if the same is 0, 1 is not the same ~ ~ ~

 

Ooh ooh, I say a little bit of simple arithmetic, and finally turn to the next big one condition N C language compiler charm of the ~ ~ ~

         Conditional compilation how to say? I'll give a few good examples, this is relatively easy to understand:

1:    #if conditional expressions

       // when the condition is satisfied, the compiled or precompiled code here

       #else conditional expression

       // when the condition is satisfied, the compiled or precompiled code here

       #else conditional expression

       // when the condition is satisfied, the compiled or precompiled code here

       #endif

2:    #ifdef macro name

       // If the macro is defined before too, compiled or precompiled code here

       #else

        // If not been defined before the macro, compiled or precompiled code here

       #endif

3:   #ifndef macro name

       // If not been defined before the macro, compiled or precompiled code here

       #else

       // If the macro is defined before too, compiled or precompiled code here

       #endif

If we want to cancel the conditional compilation based on a macro front, where you can use: #undef

4:   #define BUFFER_SIZE  100

       #if  BUFFER_SIZE > 60

       #undef  BUFFER_SIZE 

       #define BUFFER_SIZE 60      

Ooh ooh, some experts see the book says, if C program is not conditional compilation,

C program will lose a third of its charm,

Oh, you can prove how important it is conditional compilation ~ ~ ~ ~

Reproduced in: https: //www.cnblogs.com/chenxukai/archive/2010/08/01/1789880.html

Guess you like

Origin blog.csdn.net/weixin_34390105/article/details/93480298