Bit field use & middot; today is a beautiful day!

[TOC]

Bit enumerated type field alignment and the Length

Question 1

Today encounter a problem when using the bit field and then change the code, the bit field is information in the store, you do not need a full byte, but only accounted for one or a few bits. Also known as "bit segment." The so-called "bit field" is a byte or bytes of the plurality of bits divided into a plurality of portions, and specify the number of bits per domain. Each domain defines a domain name, then you can operate by directly calling domain. A type bit field definitions are as follows:

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

Because before using a direct set over, not for specific alignment, change of use is also a one down, there was no alignment problems. Since it formats the data is arranged in the other device, so there are not a numerical phenomenon at the time of testing. I went online and found the use of alignment rules as follows:

  1. 0 is a width of an unnamed bitfield under a force field which is aligned to the next boundary type, wherein the type is the type member.
  2. Is not greater than the length of the bit field specifies the type of proper length, for example, the bit field length int not exceed 32, the bit field length can not exceed eight bool
  3. No bit bit fields may domain, then it is only used as a filler or adjust the position. Unnamed bit field can not be used
  4. If the same type field of an adjacent bit field, and the bit width less than the sum of sizeof type size, the back of the field immediately before a field store until it received so far
  5. If the domain field adjacent bits of the same type, but its bit width is greater than the sum of sizeof type size, the back of the column will start a new storage unit, which offset an integral multiple of the size of its type
  6. If the type field adjacent bit field different from the specific implementation of each compiler differences, no compression taking VC6 (bit fields of different field stored in different bit byte type field), Dev-C ++ and GCC have taken compression
  7. If the domain field interspersed with non-bit field between bit field, no compression
  8. Fetch address operation can not be performed on the bit segments
  9. If bit segment representing the number of bits is 0, then this bit segment must be unknown bit field, the next bit stored starting from the next segment bit memory cell section
  10. If the bit segments appear in an expression, it will automatically update integer automatically converted to an int or unsigned int
  11. When the position of the segment assignment, it is best not to exceed the maximum range of a bit segment can be represented, as this may cause unexpected results
  12. The total size of the entire structure of the basic types of the widest member of an integer multiple of the size.

Therefore, to find the cause of error bytes is not assigned to a domain using time domain configuration bit bytes remaining bit more than the number of bits of the current bit, resulting in the new start bit field allocated in the new storage unit, data misalignment occurs (Rule 5) . The method to change the contents of an unused domain divided into two, one for the current number of bits padded in the remaining memory cells, remaining definitions another domain.

Question 2

This bit field members to use enum type definition, when most began to discover problems, doubt whether the enumeration types are not the same as using a bit field storage, so the type of unused domain names to be defined as int type, found data normal, problem solved! Under view of the very beginning of a structure, the second structure suitable complementary bit, the third bit field is unnecessary to directly measured three int size of the data structure are 5,4,4 bytes , the third rule is in line 6, is compressed, the overall size of the other multi-bit allocation structure obtained in an 8-byte (12) in the three structures whereHere Insert Picture Description

The above is achieved under keil + c + Stm32.

Original: Large column  bit field use · Today is a beautiful day!


Guess you like

Origin www.cnblogs.com/petewell/p/11445317.html