-bit operation
The need for bit operation: Connect to other devices via serial port or
& Bitwise and
| bitwise OR
^ Bitwise XOR: Two-bit dissimilarity is 1, and the phase is 0.
The above three operations meet the binding law and exchange rate.
left shift operator:<< left shift of bits to be calculated
rule: High drop, Low fill 0.
Use format:
int a = 1<<1;
A = 2;
Right-shift operator:>> to move the binary right of the operation
Rule: High-level complement sign bit, status discard.
sign bit: If positive, the sign bit is 0and the negative sign bit is 1.
Tips:
The left-shift N -bit equals 2 times the n - Square, but the efficiency is higher than the mathematical arithmetic.
The right Shift N is equal to the n -squared by 2 , but is more efficient than mathematical arithmetic.
ERROR-Proof guidelines:
Avoid bitwise operators, logical operators and mathematical operators appear in one expression
When bitwise operators, logical operators, and mathematical operators need to participate in the operation at the same time, use parentheses () as much as possible to express the order of calculation.
Embedded comparison pay attention to efficiency!
The addition and subtraction takes precedence over the left and right shifts.
swap Two variables: Replace with #define and \ code blocks
#define SWAP1 (A, b) \
{ \
int temp = A; \
A = b; \
b = temp; \
} \
#define SWAP2 (A, b) \
{ \
A = a + B; \
b = a A; \
A = a-B; \
} \
problem: If a is large andb is large, The value of a+b may cause a overflow.
#define SWAP3 (A, b) \
{ \
A = a ^ b; \
b = a ^ b; \
A = a ^ b; \
} \
Bit operators are much more efficient than plus and minus, and the integer exchange suggests a third method.
An interview question:
There is a sequence in which the natural numbers are in the form of even numbers, with only one natural number appearing in an odd number of times to find out the number.
Greedy Method --+ +,- - expression reading skills
1. each symbol handled by the compiler should contain as many characters as possible
2. the compiler reads as many characters as possible from the left like the right one
3. It is not possible to immediately make the characters read into a valid symbol with the characters that have been read.
Program compilation Four steps:
the . C and . h files undergo pre-processing CPP to produce . I files, and then through the compiler GCC , the . s file is generated, and the. o fileis generated through the assembler as,. O and . A files are generated by the linker . out files.
Pre-compilation phase:
Dispose of all comments and use spaces instead.
Remove #define , expand all macros
handle conditional compilation instructions #if, #ifdef, #elif, #else, #endif.
process #include, expand included Files
keep the #pragma instructions that the compiler needs to use
Pre-processing directive:GCC-E File.c-o hello.i
Compile phase:
A series of lexical analysis, grammatical analysis and semantic analysis of preprocessing files
Lexical analysis: keywords, identifiers, immediate numbers, etc.
Parsing: Whether an expression follows a syntax huize
Semantic Analysis: A further analysis on the basis of grammatical analysis
generate the. I file
After the analysis is finished, code optimization is generated to generate the corresponding assembly code file
compiler directive: gcc-s File.c-o Hello.s
generate A. s file.
linker: loads the. O files and library files together to generate the final executable file.
Closely related to the source code, preprocessing, compiling, compiling.
Linker
Function: The parts of each module are referred to each other well, so that each module correctly linked.
#define defined macro constants can appear anywhere in the code
#define start with the bank and the following code can use this macro constant.
#define alias original content
#define Path Usage:
#define PATH_1 D:\XYZ\test.c
#define expression has the illusion of a function call, but it is not
#define Expressions can be more powerful than functions
#define Expressions are more error-prone than functions
printf ("%d\n", (i++<j)? (i++): (j)));
The macro expression is processed during the precompiled period and the compiler does not know the existence of the macro expression
A macro expression uses "argument" to completely replace a formal parameter without any calculation.
macro expression does not have any "call" overhead
A macro expression cannot have a recursive definition
Scope of the macro definition
#define X 256
The following can be used until #undef X
#undef X
Powerful built-in macros
examples of macro Meanings
_file_ The compiled file name file1.c
_line_ Current number of rows 25
_date_ Compile-time date Jan312012
_time_ compile time :£º
_STDC_ compliance with standard C specification 1
Defining log Macros
If a function is used to write a log:
void log (char* s)
{
printf ("%s%d%s\n", _file_,_line_,s); Line
}
If you call this function in a file, you will find that the second data displayed is always the number of rows when the function is defined.
Because you want to invoke this function, you need to jump back to the function execution so that only the number of rows that define the function is printed.
You need to use the macro definition
#define LOG (s) \printf ("%s%d%s\n", _file_,_line_,s);
There's not enough information, and we need to add some time.
include the time header file first,#include "time.h"
void F ()
{
time_t T;
struct tm* ti;
Time (&t);
TI = Localtiome (&t);
printf ("%s%d%s", Asctime (TI), _line_,_file_,s);
}
How to add this function to a macro definition
#define LOG (s) do{\
time_t T; \
struct tm* ti; \
Time (&t); \
TI = localtime (&t); \
printf ("%s [%s%d],%s", Asctime (TI), _file_,_line_,s); \
}while (0);
The continuation of the Ox fork!
After-school thinking:
#define F (x) ((x)-1)
What does a macro mean? Are you sensitive to spaces? IS macro "call" sensitive to whitespace?
January 15, 2017 11:20:59 Miscellaneous