﻿# V2630\. MISRA\. Bit field should not be declared as a member of a union\.

This diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guidelines\.

This diagnostic rule is relevant only for C\.

A bit field should not be declared as a data member of a union\. The way bit fields are stored in user\-defined types is implementation\-defined\. If two bit fields with the same type are declared one after another, the compiler is not obligated to optimize their location in a single memory block\. As a result, it is not clear which bits of a previously stored value the bit field will access\.

The code example for which the analyzer issues warnings:

```cpp
union U1
{
  int a : 8;     // <=
  int b;
};

union U2
{
  int a : 8;     // <=
  int b : 24;    // <=
};
```

The rule does not apply to subobjects within a union that are not themselves unions\.

```cpp
union U3
{
  struct
  {
    int a:4;
    int b:4;
  } c;
  int d;
};
```