Does the Linux C language have bits?

05-05-2023

The editor in this article introduces in detail whether the Linux C language has bit, the content is detailed, the steps are clear, and the details are handled properly. I hope this article on whether the Linux C language has bit can help you solve your doubts. Follow the editor's thinking slowly Slow down, let's learn new knowledge together.


Linux C language has bit; in MCU C language, bit is a newly added keyword, which is often used to define a bit variable; C language defines bit type The method of data is: 1. Defined by sbit or bit; 2. Defined by bit field (in the structure); 3. Defined by combining bit operators.

C language defines bit-type data:

1. Through sbit or bit definition

sbit is mapped to IO Port (bit of IO port such as P1^1)

bit is in the bit-addressable space in RAM, and is generally used as a flag bit for program judgment.

Think of them as one external (sbit) and one internal (bit).

In the C language of single-chip microcomputer, bit is a new keyword, which is often used to define a bit variable

Second, define by bit field (in the structure)

Definition of bit field and description of bit field variable Bit field definition is similar to structure definition, its form is:

struct bit field structure name

{ bit field list};

The form of the bit field list is: type specifier bit domain name: bit field length

For example:


struct bs { int a:8; int b:2; int c:6; };


Bit field variables are declared in the same way as structure variables. There are three ways to define first and then explain, define and explain at the same time, or directly explain. For example:


struct bs { int a:8; int b:2; int c:6; }data;


Indicates that data is a bs variable, occupying two bytes in total. Among them, bit field a occupies 8 bits, bit field b occupies 2 bits, and bit field c occupies 6 bits. The definition of the bit field is as follows:

1. A bit field must be stored in the same byte, and cannot span two bytes. If the remaining space of one byte is not enough to store another bit field, the bit field should be stored from the next unit. It is also possible to intentionally make a bit field start from the next unit. For example:


struct bs { unsigned a:4 unsigned :0 /*airspace*/ unsigned b: 4 /*store from the next unit*/ unsigned c:4 }


In this bit field definition, a occupies 4 bits of the first byte, and the last 4 bits are filled with 0 to indicate that it is not used, b starts from the second byte, Occupies 4 bits, c occupies 4 bits.

2. Since the bit field is not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is to say, it cannot exceed 8 binary bits.

3. The bit field can have no bit domain name, then it is only used for filling or adjusting the position. Unnamed bit fields cannot be used. For example:


struct k { int a:1 int :2 /*The 2 bits cannot be used*/ int b:3 int c:2 };


It can be seen from the above analysis that a bit field is essentially a structural type, but its members are allocated in binary.

Second, the use of bit fields

The use of bit fields is the same as that of structure members, and its general form is: bit field variable name·bit field field allows various formats output.


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); }


In the above example program, the bit field structure bs is defined, and the three bit fields are a, b, and c. The variable bit of bs type and the pointer variable PBit pointing to bs type are illustrated. This means that bitfields can also use pointers.

Lines 9, 10, and 11 of the program assign values to the three bit fields respectively. (It should be noted that the assignment cannot exceed the allowable range of the bit field) Line 12 of the program outputs the contents of the three fields in integer format. Line 13 sends the address of the bit field variable bit to the pointer variable PBit. Line 14 uses a pointer to reassign a value to the bit field a, assigning it to 0. Line 15 uses the compound bitwise operator "&=", which is equivalent toIn: PBit->b=PBit->b&3 The original value of the bit field b is 7, and the result of the bitwise AND operation with 3 is 3 (111&011=011, the decimal value is 3). Similarly, the complex bit operation "|=" is used in line 16 of the program, which is equivalent to: PBit->c=PBit->c|1 and the result is 15. Line 17 of the program outputs the values of these three fields by means of pointers.

The main purpose of using bitfields is to compress storage. The general rules are:

1) If the types of adjacent bitfield fields are the same, and the sum of their bit widths is less than the type sizeof size, the following field will be stored next to the previous field until it cannot be accommodated;

2) If the types of adjacent bit field fields are the same, but the sum of their bit widths is greater than the sizeof size of the type, Then the following fields will start from the new storage unit, and its offset will be an integral multiple of its type size;

3) If there are non-bitfield fields interspersed between bitfield fields, no compression will be performed;

4) The total size of the entire structure is an integer multiple of the size of the widest basic type member.

Third, operate by combining bit operators


#define Setbit(x,y) (x|=(0x01<<y)) / /Position #define Clrbit(x,y) (x&=(~(0x01<<y))) //reset (clear) #define Chkbit(x,y) (x&(0x01<<y)) //Check bit



Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us