Question about setting bits

In C or C ++, it is apparently possible to limit the number of bits a variable has, for example:

unsigned char A:1; unsigned char B:3; 

I am not familiar with how this works on purpose, so a number of questions:

If I have a class with the following variables:

 unsigned char A:1; unsigned char B:3; unsigned char C:1; unsigned char D:3; 
  • What is called the above technique?
  • Is over four bytes the size or size of one byte?
  • Are variables processed as 1 (or 3) bits as shown, or as "unsigned char", which are processed like every byte?
  • Is there a way to combine bits in a centralized byte? So for example:

.

 unsigned char MainByte; unsigned char A:1; //Can this be made to point at the first bit in MainByte? unsigned char B:3; //Etc etc unsigned char C:1; unsigned char D:3; 
  • Is there an article that covers this topic in more detail?
  • If "A: 1" is treated as a complete byte, what is its dot / purple?

Feel free to mention any other considerations (e.g. compiler restrictions or other restrictions).

Thanks.

+6
source share
2 answers

What is called the above technique?

Bit fields. And you should use int ( signed , unsigned or otherwise) as a "type", not a char .

Is the class four bytes in size or one byte in size?

None. This is probably sizeof(int) , because the compiler creates a word-size object. However, the actual bit fields will be stored in bytes. It just wastes some space.

Are variables processed as 1 (or 3) bits as shown, or as an "unsigned char", processed like every byte?

They represent only the indicated bits and will be packed as tightly as possible.

Is there a way to combine bits with a centralized byte? For example:

Use union :

 struct bits { unsigned A:1; unsigned B:3; unsigned C:1; unsigned D:3; }; union SplitByte { struct bits Bits; unsigned char Byte[sizeof(struct bits)]; /* the array is a trick so the two fields are guaranteed to be the same size and thus be aligned on the same boundary */ } SplitByteObj; // access the byte SplitByteObj.Byte[0] // access a bitfield SplitByteObj.Bits.B 

Please note that there are problems with bit fields, for example, when using streams. Each bit field cannot be obtained individually, so you can get errors if you try to use the mutex to protect each of them. In addition, the order in which the fields are laid out is not clearly defined by the standard. For this reason, many people prefer to use bitwise operators to implement bit fields manually.

Is there an article that covers this topic in more detail?

Little. The first few you get when Google is all you find. They are not a commonly used design. It’s best to bite off the standard to determine exactly how they work so that you don’t bite a strange edge. I could not say exactly where in the standard they are indicated.

If "A: 1" is treated as a complete byte, what is its dot / purple?

This is not so, but I have already turned to it.

+8
source

These are bit fields.

Information about how these fields are located in memory is mainly determined by the implementation. As a rule, you will find that the compiler somehow packs them. But this can take into account various alignment problems.

+5
source

Source: https://habr.com/ru/post/896525/


All Articles