C Language Learning Tutorial eighth chapter-Enumeration, bitwise operations (5)

Source: Internet
Author: User
Tags array length definition bitwise bitwise operators variables printf

The use of a bit field is the same as that of a struct member, and its general form is: The bit field variable name • Bit domain name bit field allows 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 previous example program, and three bit fields are a,b,c. Describes the BS type variable bit and pointer variable pbit that points to the BS type. This means that a bit field can also use pointers.
The 9, 10, and 113 lines of the program assign values to three bit fields respectively. (Note that the assignment cannot exceed the allowed range of the bit field) the 12th line of the program outputs the contents of three fields in integer format. Line 13th gives the address of the bit field variable bit to the pointer variable pbit. Line 14th assigns a value to bit field a by pointer, assigned to 0. Line 15th uses the compound bitwise operator "&=", which is equivalent to: pbit->b=pbit->b&3 bit field B has a value of 7, and 3 bitwise AND operation result is 3 (111&011=011, decimal value 3). Similarly, the compound bit operation "|=" is used in the 16th line of the program, which is equivalent to: pbit->c=pbit->c|1 the result is 15. The 17th line of the program outputs the values of these three fields by pointer.

Type definition symbol typedef

The C language not only provides a rich data type, but also allows the user to define the type descriptor themselves, which means that the user is allowed to "alias" the data type. The type definition typedef can be used to complete this function. For example, there is an integral type a,b, which is described as follows: int aa,b; where int is the type descriptor of an integer variable. int is fully written as an integer,
In order to increase the readability of the program, the integer specifier can be defined by typedef as: TypeDef int INTEGER, and then integer can be used instead of int as the type description of integer variable. For example: INTEGER a,b; it is equivalent to: int a,b; The use of typedef to define arrays, pointers, structures and other types will be very convenient, not only make the program easy to write and make the meaning more clear, thus enhancing readability. For example:
typedef char NAME[20]; Indicates that name is a character array type with an array length of 20.
You can then use name to describe the variable, such as: Name A1,a2,s1,s2; exactly 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 type of struct that STU represents STU, and then STU to describe the structure variable: STU body1,body2;
The general form of the TypeDef definition is: TypeDef The original type name the new type name contains the definition part in the original type name, and the new type name is generally capitalized to
Easy to distinguish. A macro definition is sometimes used to replace the functionality of a TypeDef, but the macro definition is done by preprocessing, while the TypeDef is done at compile time, which is more flexible and convenient.

Summary of this chapter

1. Enumerations are a basic data type. Enumeration variables have a finite value, and enumeration elements are constants, not variables.

2. Enumeration variables are usually assigned by assignment statements, not by dynamic input. Enumeration elements can be defined by a system or user as a sequential value, but enumeration elements and integers are not the same, and they belong to different types. Therefore, you cannot use printf statements to output element values (which can be output sequential values).

3. Bit operation is a special operation function of C language, it is operation in bits unit. Bitwise operators are only logical operations and shift operations two classes. The bitwise operator can be combined with an assignment to form a composite assignment character. such as &=,|=,^=,>>=,<<= and so on.

4. The use of bit operation can complete the assembly language of some functions, such as position, bit clear zero, displacement and so on. It can also be used to compress and store data and parallel operations.

5. A bit field is essentially a struct type, but its members allocate memory by bits. Its definition, description and use of the same way as the structure.

6. Bit domains provide a means to compress data in high-level languages, save storage space, and improve program efficiency.

7. The type definition typedef provides users with a means of customizing the type specifier, taking care of the user's habit of using vocabulary in programming and increasing the readability of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.