Water drop stone wearing C language C language underlayer

xiaoxiao2021-03-06  15

Overview

The C language memory model basically corresponds to the actual storage model of Von Neumann (von Nuiman) computer, which has reached a map of the machine. This is the main reason for C / C is suitable for the underlying development. In addition, There is another reason for the C language suitable for the underlying development, that is, the C language has made a lot of support for the underlying operation, providing a lot of comparative underlying functions.

The following combinations are set forth in combination.

Question: Shift operation

Two problems must be clear when using shift operators:

(1) In the right shift operation, the sprinkle is filled in the 0 or symbolic position;

(2) What number can be shifted.

Answer and analysis:

">>" and "<<" refers to moving each of the points or left to the left, and its usual form is:

Right movement: Variable name >> Displacement number

Left shift: Variable name << Displacement bit

After shifting, the bit of one end is "squeezed" and the other end is fill it in 0, and the shift in the C language is not looped.

(1) The answer to the first question is simple, but it is necessary to determine according to different situations. If the shift is unsigned, it is filled. If it is a symbol number, it may be filled with 0 or symbol. If you want to solve the problem of the fill in the right shift in the right shift, declare the variable as an unsigned type, so that the vacancy will be set 0.

(2) The answer to the second question is also very simple: if the N bit is moved, then the number of digits of shift is not less than 0, and must be less than n. This will not remove all data in one operation.

For example, if the integer data accounts for 32 bits, n is a integer data, then n << 31 and n << 0 are legal, and n << 32 and n << -1 are illegal.

Note that even if the sprinkle is filled, the right shift of the symbol integer is not quite except. To prove this, we can think about -1 >> 1 impossible to be 0.

Problem: Segment Structure

Struct rpr_atd_tlv_header {ulong res1: 6; Ulong Type: 10; Ulong Res1: 6; Ulong Length: 10;

The bit segment structure is a special structure that the bit structure is more convenient than the bit-bit operator when accessing one byte or word.

The general form of bit structure definition is:

Struct bit structure name {data type variable name: integer constant; data type variable name: integer constant;} bit structure variable

Among them: The integer constant must be non-negative integers, the range is 0 ~ 15, indicating the number of binary positions, that is, how many bits are indicated.

The variable name is the selection, which may not be named, so that it is to arrange the need.

For example: one bit structure is defined below.

Struct {Unsigned Incon: 8; / * Incon occupies 0 ~ 7 total 8-bit * / unsigned TxColor: 4; / * TxColor occupies a high zip of 0 ~ 3 digits * / unsigned BGColor: 3; / * BGColor occupies 4 ~ 6 digits of high bytes of 3 * / unsigned blink: 1; / * blink occupies the 7th bit of high bytes * /} CH;

The access of the membership member is the same as the accesses of structural members.

For example: The BGColor member in the upper case structure can be written:

ch.bgcolor

Binding members can be used with other structures. By bit access and setting, convenient & saving

E.g:

Struct info {char name [8]; int Age; struct addr address; float pay; unsigned state: 1; unsigned pay: 1;} workers;

The structure of the above example defines information about one work. There are two part structures, and each bit structure member is only one, so only one byte but saves two information. The first one represents the state of the worker. The second represents whether the wage has been issued. . This shows that the use bit structure can save storage space. Be careful not to exceed the value limit

Question: Byte alignment

In the process of using the VC programming, when you call the structure defined in the DLL, the foundation structure is chaotic, you can't read the correct value at all, then discover this because the DLL and the byte alignment options used by the calling program So what I want to ask, what is the byte?

Answer and analysis:

About byte alignment:

1. When different structures use different bytes to align, it may cause interaction between them.

2. When communicating across the CPU, you can use byte alignment to ensure uniqueness, such as the communication protocol, the structure of the register, and the like.

Three aligned methods:

1. Natural Alignment: The size of the data type is equal.

2, specify alignment:

#pragma pack (8) // Specify align to 8; #pragma pack () // Restore to its original value

3, actual alignment:

Actual align = min (Order Align, Natual Align)

For complex data types (such as structures, etc.): The actual alignment is the most actual alignment of its members:

Actual align = max (actual align1, 2, 3, ...)

Filling rules of the compiler:

1. The member is an integer multiple of the member Actual Align, plus padding in front.

Member Actual Align = Min (Structure Actual Align, Set Alignment)

2, the structure is an integer multiple of structure Actual Align, plus padding behind.

Example analysis:

#pragma pack (8) // Specify align to 8Struct Stest1 {Char Ch1; Long Lo1; Char CH2;} test1; #pragma pack ()

right now

Align of Stest1 = 4, sizeof stest1 = 12 (4 * 3)

The arrangement of TEST1 in memory is as follows (ff is padding):

00 - - - 04 - - - 08 - - - 12 - - 01 FF FF 01 01 01 01 01 FF FF CH1 - LO1 - CH2 # prgma pack (2) // Specify Align to 2Struct Stest2 {char CH3; Stest1 Test;} test2; #pragma pack ()

Now Align of Stest1 = 2, Align of Stest2 = 2, SizeOf Stest2 = 14 (7 * 2)

The arrangement of TEST2 in memory is as follows:

00 - - - 04 - - - 08 - - - - - - - - - - - - - - - - - - - - - - - - -

Precautions:

1. In this way, the compiler cannot be optimized for a particular platform. If the efficiency is very important, try not to use #pragma pack, if necessary, it is best to set it only in the required place.

2, where you need to add PACK must be added to the header file of the structure, do not rely on the command line option, because if many people use the header file, not everyone knows Pack. This is particularly manifested when a library file is developed for others, if a library function uses Struct as its parameters, when the caller and library file developer use different PACK, it will cause errors, and this class error is very bad. 3. In the header file provided in VC and BC, in addition to aligning the structure on the four-byte structure, it has added PACK, otherwise, the Windows program we have compiled will not run normally.

4. Do not include other header files after #pragma pack (n). If the Align value is changed in the included header file, unpredictable results will be generated.

5. Don't define a data structure while multiple people. This ensures a consistent PACK value.

Problem: Bittage operator

The C language and other advanced languages ​​are different that it fully supports the bitmap operator. This is similar to the bit operation of the assembly language. The bit bit operator in C is listed below:

━━━━━━━━━━━━━━━━━━ 操作 操作 操作━━─── 操作 操作 操作 操作── - ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────> & bit logic and | bit logic or ^ bit logic varies or - bit logic anti >> Right shift << Left shift ━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━

note:

1. The bitwise operation is to detect, set, or shift the actual bits in bytes or words, which only apply to characters and integer variables and their variants, which are not applicable to other data types.

2, the results of the relationship operation and logical operation expressions can only be 1 or 0. The result of the standing operation can take a value other than 0 or 1. It should be noted that the difference between the bit operator and the logical operator, for example, if x = 7, the value of X && 8 is true (two non-zero values ​​are still non-zero), and the value of X & 8 is 0.

3, | with ||, & &&, ~!

&, | And ~ Operators Take their operands as a sequence, stand up separately. For example: 10 & 12 = 8, this is because "&" operators 10 and 12 are used as binary descriptions 1010 and 1100, so only when the same bits of the two operands are simultaneously 1, the corresponding position To 1. Similarly, 10 | 12 = 14 (1110), by complementing operation, ~ 10 = -11 (11 ... 110101). &&, || and! The operator treats their operands as "true" or "false", and uses 0 to represent "false", any non-0 value is considered "true". They return 1 represent "true", 0 represent "false", for "&&", "& ||" operators, if the value of the operands on the left can determine the value of the expression, they do not calculate the right side. Operations. Therefore,! 10 is 0 because 10 is not 0; 10 && 12 is 1, because 10 and 12 are neither 0; 10 || 12 is also 1, because 10 is not 0. Also, in the last expression, 12 is not calculated at all, and in the expression 10 || f ().

转载请注明原文地址:https://www.9cbs.com/read-45950.html

New Post(0)