When storing some information, it does not need to occupy a full byte, but only needs to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit. To save storage space and simplify processing, the C language also provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, several different objects can be represented by a byte binary field. 1. Definition of a bit field and description of a bit field variable the definition of a bit field is similar to that of a structure, in the form:
Struct bit domain structure name
{Bit domain list };
The format of the bit domain list is: type description Character Domain Name: Bit domain Length
For example:
Struct bs
{
Int a: 8;
Int B: 2;
Int c: 6;
};
The description of bitfield variables is the same as that of structure variables. You can first define and then describe, and define or directly describe these three methods. For example:
Struct bs
{
Int a: 8;
Int B: 2;
Int c: 6;
} Data;
It indicates that data is a bs variable, which occupies two bytes in total. Where a occupies 8 places, B occupies 2 places, and c occupies 6 places. The definitions of bit domains are described as follows:
1. A single-byte field must be stored in the same byte, and cannot span two bytes. If the remaining space of one byte is insufficient to store another domain, it should be stored from the next unit. You can also intentionally start a domain from the next unit. For example:
Struct bs
{
Unsigned a: 4
Unsigned: 0/* airspace */
Unsigned B: 4/* stored from the next unit */
Unsigned c: 4
}
In the definition of this bit field, a occupies 4 bits in the first byte, And the last 4 bits fill in 0 to indicate that it is not used. B starts from the second byte and occupies 4 bits, and c occupies 4 bits.
2. A bit domain can be a non-bit domain name. In this case, it is only used for filling or adjusting the position. An anonymous domain cannot be used. For example:
Struct k
{
Int a: 1
Int: 2/* The two digits cannot be used */
Int B: 3
Int c: 2
};
From the above analysis, we can see that the bit field is essentially a structure type, but its members are allocated by binary.
In short, simple words
This is the representation of bitfield operations. That is to say, ": 1" is added to indicate that the size of the member occupies 1 bit of the defined type, and ": 2" occupies 2 bit, and so on. Of course, the size cannot exceed the total number of BITs contained in the defined type.
A bytes (byte) is 8 bit ). For example, the type defined in your structure is u_char, one byte, 8 bits in total, and the maximum value cannot exceed 8.
32-bit server,
Short is a string of 2 bytes, with a total of 16 bits. The maximum value is 16.
Int Is 4 bytes, a total of 32 bit, the maximum cannot exceed 32.
And so on.
This definition saves space. For example, in the above structure, the variable type defined is u_char, which is a byte type, that is, 8bit.
Fc_subtype occupies 4 bits, fc_type occupies 2 bits, and fc_protocol_version occupies 2 bits, 8 bits in total, exactly one byte.
Each of the other eight members occupies 1 bit, 8 bit in total, which is exactly one byte.
Therefore, if the size of your structure is calculated using sizeof (struct frame_control), it is 2 bytes.
Bytes --------------------------------------------------------------------------------------------
Colon in the struct
Today, we can see that the CRC bit algorithm learns the struct definition in C ++ and can use ":". The details are as follows:
In the standard C language, the following statements are valid:
Struct test {
Int a: 3;
};
In this way, after a value is assigned, only the last 3bit is taken, that is, the value is converted to a value between-4 and 3. For example:
Value assignment-5-4-3-2 2 3 4 5
Actual value: 3-4-3-2 2 3-4-3
If a is unsigned, it is converted to 0 to 7. It is also applicable to other types of non-int type (only the number after ":" and the result of a must be an integer ).
Here, the value is '-5 (dec)' = 1101 (original) = 1011 (supplemented). Therefore, after the last three digits are retrieved, the value is changed to '3 (dec )', of course, in the struct definition of CRC operation, only the last bit is used. The specific definition is as follows:
Typedef unsigned char bit;
Typedef unsigned char byte;
Typedef unsigned short 2010;
Typedef union {
2010val;
Struct {
B2bit0: 1;
B2bit1: 1;
B2bit2: 1;
B2bit3: 1;
B2bit4: 1;
B2bit5: 1;
B2bit6: 1;
B2bit7: 1;
B2bit8: 1;
B2bit9: 1;
B2bit10: 1;
162bit11: 1;
B2bit12: 1;
B2bit13: 1;
B2bit14: 1;
B2bit15: 1;
} Bits;
} CRCREGS;
PS: I will try my best to write my learning experience in the future, so as not to forget it later. :) of course, some logic design circuit diagrams are really hard to draw. I will write them on NOTEBOOK...