1. The third variable is not required. The method for exchanging two variables is as follows:
A = a ^ B;
B = a ^ B;
A = a ^ B; // (a ^ B) ^ A = B
Or
A = A + B;
B = A-B;
A = A-B; // (a + B)-A = B
(Note: The above two methods do not adapt to floating point variables, indicating the reason for the method and accuracy)
2. Calculate the number of '1' in a variable:
F (char X ){
For (INT I = 0; X & = X-1, I ++ );
Return I;
}
Explanation: Assume X is 11010100.
X-1 is 11010011
X & (X-1) is 11010000. Here, we find that the result is not zero and 1 is missing. Therefore, if one loop is less than one, record the number of cycles.
In addition, if a number is the N power of 2, it must be 1 followed by 0, such as 1000,100.
If the result of X & (X-1) is zero, it is the N power of 2.
3. Use Function macros like Functions
/* Macro definition */
# Define macro (c )\
Do {\
If (c )\
{\
Printf ("Hello! ");
}\
} While (0) // After the do-while statement while (), there should be a ";". Here, we intentionally remove it and the condition is always false, that is, the internal statement can only be executed once.
/* The following is macro usage */
Macro (1); // The ";" here looks like a semicolon at the end of a statement. In fact, it is the semicolon rounded up at the end of the macro definition. Such a do-while macro is used like a function.
4. If statements without the if Class
(Void) (dll_exit = dll_zero_info & fputs ("size of address record is zero. \ n", stderr ));
Using the short circuit principle of the & operator, the value of the forward expression can determine whether the backend can be executed.
5. curly braces enable macro to return values.
Returns the value assignment result of the last statement.
# Define even (x )\
({\
Int y = x ;\
(2 * (y/2) = y? Y: Y + 1 );\
})
Http://topic.csdn.net/u/20091126/20/e05094ad-448e-41a6-a199-f3ca93d40c43.html? Seed = 222192752 & R = 79877196 # r_79877196
In other words, curly braces are used to replace the do {} while (0) statement, so that the macro can add ";" after use.
Suppose that the block value represented by curly brackets is the value of the last statement in curly brackets. (To be verified)
6. FUNCTION macro-defined return values
# Include
<Stdio. h>
# Define
Even (x, RET )\
{\
Int
Y = x ;\
RET
= (2 * (y/2)
= Y
? Y: Y + 1 );\
}
Int main (void)
{
Int
RET;
Int
Num =
10;
Even (Num, RET );
Printf ("Num
= % D \ n ", RET );
Num ++;
Even (Num, RET );
Printf ("Num
= % D \ n ", RET );
Return
0;
}