The smallest unit of access to data in memory is usually bytes, but sometimes it does not require a single byte to store. For example, if only two of the true and false are worth a variable, you just need one. For example, in some cases memory is extremely valuable resources, such as some embedded devices, so pay special attention to saving. The C language can store data in a single structure in bits, which are called bit fields or bits.
struct Bit_data
{
int a:2;
char c:3;
};
While the bit-domain mechanism provides a way to use a non-byte memory, the bit domain is used with some attention to the problem, for example, the length of a bit domain member cannot exceed the length of its own declaring type char cannot exceed 8, the following are the two main problems:
(1) The problem of member values
Declare a bit field as follows
struct BOOL
{
int a:1;
};
Want to declare one such bit domain to use him as a bool, but the actual situation is not the same as imagined. Given 1, the output is realistically-1. This is because in the binary complement, the most significant bit is also called the sign bit, when set to 1 o'clock, a negative number, and when set to 0 o'clock, the value is non-negative. There is only one person, then he is the most effective bit, then 1 he is a negative number, here is 1, and 0 is 0 this non-negative number. The following is a complete test procedure.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct inti{
int a:2;
};
struct Inti data;
int i;
for (i = 0; i < 4; i++)
{
data.a = i;
printf ("%2d", data.a);
printf ("%u\n", DATA.A);
}
return 0;
}
The output is as follows:
0 0
1 1
-2 4294967294
-1 4294967295
The representation of the assigned value
| Assigned value |
Binary representation |
In-memory value (32bit) |
Signed values |
unsigned values |
| 0 |
00 |
0x00000000 |
0 |
0 |
| 1 |
01 |
0x00000001 |
1 |
1 |
| 2 |
10 |
0xFFFFFFFE |
-2 |
4294967294 |
| 3 |
11 |
0xFFFFFFFF |
-1 |
4294967295 |
Calculating the values of their binary representations according to their complement of signed numbers is a symbolic value. This should be the reason for the first column in the output, and the output of the second column should also be related to the allocated memory.
(2) Memory allocation and alignment
1, contains only one basic type. The base type of the declaration is aligned in a bit field, such as Char on a 32-bit machine (word length), assigned 1byte. int distribution 4byte. not exceeding the length of a basic type, assigning a base type length, exceeding the n times of its basic length, and the difference between N and the actual length of the bit field less than a basic length.
struct bit1{
int a:2;
int b:2;
};
sizeof (struct bit1) returns 4;
struct bit2{
int a:18;
int b:18;
};
sizeof (struct bit2) returns 8;
2, contains a variety of basic types. The length of the longest base type declared in a bit field is for it, for example, a bit field that contains two basic types of char and int, aligned to the length of int. will be pieced together to conserve memory. Like what:
struct bit3{
int e:2;
Long long d:36;
int a:32;
int b:32;
char c:7;
};
sizeof (struct bit3) returns 20.
Summary (normal 32-bit PC)
(1) Using the bit field to implement the bool variable is actually the same as using an int type. Does not save memory, so trying to use a single bit field does not save memory, if declaring an array of bit fields is another case.
(2) The compiler to optimize the memory structure, will be aligned, this process is not to save memory. With enough memory, the compiler is more focused on the speed of access than on saving memory.