- Introduction to Bit Segments
- Example of use of a bit field.
- Memory Allocation of Bit Segments
- Example
- Memory Allocation Analysis.
## Bit segment
Introduction to Bit Segments
Bit segments (binary bits): that is, stored by bits
bandwidth(bit-field) is a special data type in C that allows a byte to be divided into parts and a specific number of bits assigned to each part in order to store and access those parts in memory.
Bit segments have two special features compared to structures.
- The members of the bit segment must be int unsigned int or signed int char(Integer family) The members of the bit segment in C99 can be of other types as well.
- The name of the member of the field is followed by a colon and the number
Example of use of a bit field.
struct S
{
int a : 2; // limited to 2 bits
int b : 5; // limited to 5 bits
int c : 10; //limited to 10 bits
int d : 30; // limited to 30 bits
}
Memory Allocation of Bit Segments
1. The bit segment is spatially opened in 4 bytes (int) or 1 byte (char) as needed. (... (e.g., int once opened 4 bytes of 32 bits to use, not enough to open up again)
2. Bit segments involve a lot of uncertainty, bit segments are not cross-platform, programs that focus on portability should avoid using bit segments. (Network programming involves, network transmission of packets,)
(Reason: there is no C standard and compilers vary.)
Bit segments cannot be larger than 32 (on 32-bit machines), and 16 bits cannot be larger than 16 bits
Defaults to bytes if not given a location segment
Example
struct S
{
char a : 3;
char b : 4;
char c : 5;
char d : 4;
};
int main()
{
struct S s = { 0 };
= 10;// 1010 | 010
= 20;//10100 | 0100
= 3; // 011 |00011
= 4; // 100 | 0100
return 0;
}
Memory Allocation Analysis.
1. The VS compiler requests space from left to right, 1 byte/8 bits at a time.
-----> Direction of space request ---->.
0000 0000
2. then start to store a data 10(D) = 1010(B), a limit of 3 bits, extra bits will be discarded, i.e., the last reserved data is 010(B), --- in vs, every bit of data will be discarded.
--- In vs, data is written from right to left in each byte.
(address) 0000 0|010
3. after put a, start to put b = 20(D) = 10100(B), b limit to 4 bytes, cut b, get b = 0100(B)
After the first space to put a, there are still 5 bits left, the group to accommodate b, so in the four bytes from the beginning (counting from right to left), write b
(address) 0010 0010(b) = 22(h).
(division) 0 | 0 1 0 0 | 0 1 0
b a
4. Next put c, c occupies 5 bits, obviously the first byte is not enough to put, so we need to open the second byte, and then cut c (not more than so do not need to cut), to get c = 011(b); (address) 0010 0010(b) = 22(h); (division) 0 | 0 1 0 0 | 0 1 0 b; (division) 0 | 0 1 0 b
(address) 0010 0010 0000 0011
(division) 0 | 0 1 0 0 | 0 1 0 0 0 0 0 | 0 0 0 0 1 1
round b a c
5. Next put d, d accounted for 4 bytes, obviously the second byte is not enough to put, so the application of the third byte, and then cut b, to get 100 (b).
(address) 0010 0010 0000 0011 0000 0100
(division) 0 | 0 1 0 0 | 0 1 0 0 0 0 0 | 0 0 0 0 0 1 1 0 0 0 0 | 0 1 0 0 0
Rounding b a Rounding c d
6. Finally converted to hexadecimal, get
22 03 04 (hexadecimal)
That is, the memory display: 22 03 04
7. There may also be memory alignment, 32-bit 22 03 04 00 ....