Word Source VC World - C language classroom
Self-archiving, also dedicated to the same Delphi programmer as me
Enumeration, bit operation
enumerate
The value of the variable is limited to a limited range. The type of enumeration is a basic data type, not a type of constructor because it cannot decompose into any basic type.
Description of the definition of enumeration type and enumeration variable
I. Definition of enumeration enumeration type definition The general form is: ENUM enumeration name {enumeration value table}; all available values should be listed in the enumeration value table. These values are also known as enumeration elements. For example: Enum weekday {Sun, Mou, Tue, WED, THU, Fri, Sat}; this enumeration is known as Weekday, the enumeration value has a total of seven days. Any value that is described as a Weekday type variable can only be a day in seven days.
Second, the instructions of enumeration variables are like structures and combined, and the enumeration variables can also be described in different ways, that is, first defined, and the description is defined or directly explained. With variables A, B, C, which are described as the above-mentioned WeekDay, can be used: Enum weekday {...}; Enum weekday a, b, c; or: enum weekday {. .....} a, b, c; or: enum {...} a, b, c;
Assignment and use of enumerated type variables
Enumeration Types There are the following provisions in use: 1. Enumeration value is constant, not a variable. You cannot use the assignment statement in the program to assign it. For example, the elements that enumerate Weekday are then assigned: sun = 5; MON = 2; Sun = MON; is incorrect.
2. The enumeration element itself defines a value representing the number of sequence numbers from the system, from 0 to 0, 1, 2 .... As in Weekday, the Sun value is 0, the MON value is 1, ..., the SAT value is 6. Main () {Enum weekday {Sun, MON, TUE, WED, THU, FRI, SAT} A, B, C; a = sun; b = mon; c = tue; printf ("% D,% D,% D ", A, B, c);} 3. Only give enumeration variables to enumerate enumeration values, and cannot give an enumeration variable directly. Such as: a = sum; b = mon; is correct. And: a = 0; b = 1; it is wrong. If you must give the value to the enumeration variable, you must convert the mandatory type, such as: a = (enum weekday) 2; its meaning is to give an enumeration element of the sequence number 2 to the enumeration variable A, equivalent to: a = Tue; It should also be explained that the enumeration element is not a character constant is not a string, not adding a single, double quotes. Main () {ENUM body {a, b, c, d} month [31], j; int i; j = a; for (i = 1; i <= 30; i ) {MONTH [i] = j; J ; if (j> d) j = a;} for (i = 1; i <= 30; i ) {switch (Month [i]) {Case A: Printf ("% 2d% C / T", I , 'a'); Break; Case B: Printf ("% 2D% C / T", I, 'b'); Break; Case C: Printf ("% 2D% C / T", I, 'C' Break; Case D: Printf ("% 2D% C / T", I, 'D'); Break; Default: Break;}}}}}}} }printf ("/ n");}
Bit operation
The various operations described above are based on bytes as the most basic bit. However, in many system programs, you often require operational or processing at a bit (bit) level. The C language provides the function of the bit operation, which makes the C language can also be used to write system programs like assembly languages. First, the bit operator C language provides six bit operators: & bit bit and | Bit or ^ Pressibility or ~ Reverse << Left shift >> right shift 1. Bit and operation standby and operator "&" Is a binocular operator. Its function is to participate in the two corresponding binary phases of the calculation. Only when the corresponding two bins are 1, the result is 1, otherwise 0. The number of participation in the calculation appears. For example: 9 & 5 can be writable as follows: 00001001 (9 binary complement) & 00000101 (5 binary complement) 00000001 (1 binary complement) Visible 9 & 5 = 1.
Bit and operations are usually used to clear certain digits or hold certain bits. For example, a high eight digits of A, reserve low eight bits, can be used as A & 255 operation (255 binary number to 0000000011111111). Main () {Int a = 9, b = 5, C; C = A & B; Printf ("A =% D / NB =% D / NC =% D / N", A, B, C);}
2. Bit or operator stand or operator "|" is a binocular operator. Its function is to participate in the two corresponding binary phases or in the calculation. As long as the corresponding two bits have one to 1, the result is 1. The two numbers participating in the calculation have occurred with complement. For example: 9 | 5 can be writable as follows: 0000001001 | 000001010000001101 (decimal 13) Visible 9 | 5 = 13mAin () {INT A = 9, B = 5, C; C = A | B; Printf ("a =% D / NB =% D / NC =% D / N ", A, B, C);}
3. Pressibility or arithmetic press or operator "^" is a binocular operator. Its function is to participate in the two corresponding two bits of the calculation or, when the two correspondence is different, the result is 1. The number of participation operations still appears in complement, for example, 9 ^ 5 can be written as follows: 00001001 ^ 00000101 00001100 (decimal 12) Main () {INT A = 9; a = a ^ 15; Printf ("A =% D / N ", a);
4. Ask the counter operation to seek anti-operator ~ is a single-grade operator, with right binding. Its function is to reverse the two bits of the number of participating calculations. For example, the calculation of ~ 9 is: ~ (0000000000001001) Results: 1111111111110110
5. Left-shift operation left shift operator "<<" is a binocular operator. Its function puts all the two bits of the number of arithmeters on the left of the "<<", and the number of mobile bits is specified by the number of "<<" on the right, and the high position is discarded. For example: A << 4 means moving 4 bits to the respective bits of A. Such as A = 00000011 (decimal 3), the left shift 4 bits is 00110000 (decimal 48). 6. Right shift operation right shift operator ">>" is a binocula operator. Its function is to put all the two bits of the number of calculations on the left side of the left, ">>" Specify the number of mobile bits. For example, set a = 15, A >> 2 means that the 0000001111 shift to 00000011 (decimal 3). It should be noted that for the number of symbols, the symbol bit will move along with the right shift. When a positive number, the highest bit is added to 0, while the negative number is 1, the symbol bit is 1, the highest bit is 0 or supplement 1 depends on the compilation system. Turbo C and many systems are set to supplement 1. Main () {Unsigned A, B; Printf ("INPUT A Number:"); Scanf ("% D", & A); B = a >> 5; b = B & 15; Printf ("A =% D / TB = % D / N ", A, B);} Please see another example! main () {char A = 'a', b = 'b'; int P, c, d; p = a; p = (P < <8) | B; D = P & 0xFF; C = (p & 0xff00) >> 8; Printf ("A =% D / NB =% D / NC =% D / ND =% D / N", A, B, C , d);} bit
Some information does not need to take a complete byte while storing, but only a few or one binary bit. For example, only 0 and 1 in two states are stored in one switch, it can be used in one binary. In order to save storage space and make it easy, the C language provides a data structure called "bit" or "bit". The so-called "bit" is divided into several different regions in one byte and indicates the number of bits in each area. Each domain has a domain name that allows you to operate by domain name in the program. This allows several different objects to be represented by one byte binary bit field. I. Definition of the bit domain and the bit domain definition of bit domain variables are similar to the structural definition, the form is: Struct bit domain structure name {bit domain list}; where the bit domain list is: Type Description Domain Name: Bit Domain length, for example,: struct bs {Int A: 8; INT B: 2; INT C: 6;}; The description of the bit domain variable is the same as the structural variable description. These three methods can be defined or directly illustrated by first defined. For example: struct BS {Int A: 8; INT B: 2; INT C: 6;} DATA; Description Data is a BS variable, a total of two bytes. The bit domain A accounts for 8 bits, and the bit domain B accounts for 2 digits, and the bit field C accounts for 6 bits. There is still the following description for the definition of the bit domain:
1. A bit domain must be stored in the same byte and cannot cross two bytes. If the space remains in one byte is not stored in another domain, the bit field should be stored from the next unit. It is also possible to intention to start from the next unit from the next unit. For example: Struct BS {Unsigned A: 4Unsigned: 0 / * Null Domain * / Unsigned B: 4 / * Start storage from the next unit * / unsigned C: 4} In this bit domain definition, a accounts for 4 of the first byte 4 Bit, the last 4 digits are not used, b begins with the second byte, occupying 4 bits, C occupying 4 bits. 2. Since the bit field is not allowed to cross two bytes, the length of the bit field cannot be greater than one byte length, that is, it cannot exceed 8 bits of bin.
3. The bit domain can have a domain name, then it is only used to fill or adjust the position. The unknown bitmade is not available. For example: struct k {int A: 1INT: 2 / * This 2 bits cannot be used * / int B: 3Int C: 2}; from the above analysis, it can be seen that the bit domain is in nature is a structural type, but its member It is assigned according to the binary.
Second, the use of the bit domain used and the use of structural members is the same, and the general form is: the bit domain variable name. The bit domain name is allowed to be output in various formats. MAIN () {struct BS {unsigned A: 1; Unsigned B: 3; Unsigned C: 4;} bit, * pbit; bit.a = 1; bit.b = 7; bit.c = 15; printf ("% D,% D,% d / n ", bit.a, bit.b, bit.c); pbit = & bit; pbit-> a = 0; pbit-> b & = 3; Pbit-> c | = 1; Printf ("% D,% D,% D / N", Pbit-> a, pbit-> b, pbit-> c);} The bit domain structure BS is defined in the above example, and the three bit are A, B, c. Describes the BS type variable bit and points to the BS type pointer variable PBIT. This means that the bitmaster is also a pointer. The 9, 10, 11 lines of the program give three bit domains. (Pay attention to assignment cannot exceed the allowable range of this bit field) The 12th line outputs three domains in an integer format. The 13th line gives the address of the bit domain variable bit to the pointer variable PBIT. The 14th line pointer is given to the domain A re-assapore, assigns to 0. The 15th line uses the composite bit operator "& =", which is equivalent to: pbit-> b = pbit-> B & 3 domain B Central original value of 7, and 3 position bit and calculation result is 3 ( 111 & 011 = 011, decimal value is 3). Similarly, the composite bit operation "| =" is used in the 16th line of the program, equivalent to: Pbit-> C = Pbit-> C | 1 The result is 15. Procedure 17 The value of these three domains is output by the pointer mode.
Type Defining Typedef
The C language not only provides a wealth of data type, but also allows yourself to define type specifiers yourself, that is, allowing "alias" by the user to take the data type. Type Defining TypeDef can be used to complete this feature. For example, there is an integer amount a, b, which describes the following: Int Aa, B; where int is a type of integer variable. INT's complete way of writing is Integer, in order to increase the readability of the program, you can define the integer specifier as: typedef int inteer can be used instead of INTEGER to instead of intimidation variables. For example: INTEGER A, B; it is equivalent to: int A, b; define an array, pointer, structure, etc. will bring great convenience, not only make the program to write simple and make sense more clear, but it is enhanced readability. For example: typedef char name [20]; indicates that Name is a character array type, and the array length is 20. The variables can then be used, such as NAME A1, A2, S1, S2; completely equivalent to: CHAR A1 [20], A2 [20], S1 [20], S2 [20] Another example: Typedef struct stu {char Name [20]; int Age; CHAR SEX;} stu; defines the STU representation of the STU, then the STU can be used to explain the structural variables: STU Body1, Body2; TypeDef defined general form is: TypeDef original type new type name The original type name contains a definition part, and the new type name is generally represented by uppercase to facilitate difference. Sometimes the macro definition can be used instead of TypeDef, but the macro definition is done by the pre-processing, and TypeDef is completed at compile, and the latter is more flexible. chapter summary
1. Enumeration is a basic data type. The value of the enumeration variable is limited, and the enumeration element is constant, not a variable.
2. Enumeration variables typically assign values by assigning statements, not by dynamic input. Although enumeration elements can define a sequential value by a system or user, an enumeration element and an integer are different, and they belong to different types. Therefore, the element value (output order value) cannot be output by the PRINTF statement.
3. The bit is calculated as a special calculation function in the C language, which is calculated by binary position. The bit operator only has two types of logical operations and shift operations. The bit operator can form a composite assignment with the assignment. Such as & =, | =, ^ =, >> =, << =, etc.
4. Use the bit operation to complete certain functions of assembly language, such as setting, zero, shift, and so on. Data can also be compressed and parallel.
5. The bit domain is also a structural type in nature, but its members are allocated in binary positions. Its definitions, instructions and methods are the same as the structure.
6. The bit field provides a means so that data can be compressed in advanced languages, saving storage space while also improving the efficiency of the program.
7. Type Definition TypedEF provides users with a means of customized type specifiers, and take care of the habit of user programming using vocabulary, and adds the readability of the program.