C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (14)

Source: Internet
Author: User
Tags bitwise operators constant integer

The

Second, bit structure
bit structure is a special structure that is more convenient than bitwise operators when you need to access multiple bits of a byte or word by bit. The general form of a
bit structure definition is:
struct the bit structure name {
Data type variable name: integer constant;
Data type variable name: integer constant; The
} bit structure variable;
Where: The data type must be int (unsigned or signed). Integer constants must be non-negative integers, and the range is 0~15, representing the number of bits, that is, how many bits. The
variable name is a selection and may not be named so that the requirement is ordered.
For example: The following defines a bit structure.
struct{
Unsigned incon:8; /*incon occupies a low byte 0~7 total of 8 bits/
unsigned txcolor:4;/*txcolor occupies a total of 4 bits of the high byte * * *
Unsigned bgcolor:3;/*bgcolor occupies a high byte 4~6 bit of a total of 3-bit * *
unsigned blink:1; /*blink occupies a high byte of the 7th bit */
}ch;
The access of a bit structure member is the same as that of a struct member.
For example: access to the bgcolor member in the previous example bit structure can be written as:
Ch.bgcolor

Attention:
1. A member in a bit structure can be defined as unsigned or signed, but it is considered a unsigned type when the member length is 1 o'clock. Because a single bit cannot have a symbol.
2. Members in a bit structure cannot use arrays and pointers, but a bit structure variable can be an array and a pointer, and if it is a pointer, its members are accessed in the same way as the struct pointer.
3. The total length of the bit structure (bits) is the sum of the digits defined by each bit member and can exceed two bytes.
4. A bit structure member can be used with other struct members.
For example:
struct info{
Char Name[8];
int age;
struct addr address;
float pay;
unsigned state:1;
unsigned pay:1;
}workers; '
The structure of the previous example defines information about a worker. There are two bit structure members, each with only one digit, so that only one byte is saved but two information is stored, the first in the byte indicates the status of the worker, and the second indicates whether the pay has been paid. This shows that the use of bit structure can save storage space.

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.