MISRA Discussion Forums
Resolve Rule 5-0-6: cast to 7 bits struct field - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.4 Standard conversions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=133)
+---- Thread: Resolve Rule 5-0-6: cast to 7 bits struct field (/showthread.php?tid=1097)



Resolve Rule 5-0-6: cast to 7 bits struct field - pvarela - 12-08-2014

How can resolve next Notes??

Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-6, Implicit conversion of integer to smaller type
Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-3, Implicit conversion of integer cvalue expression


In this assignament:
CTU.EVTCFGR[fTriggerChannel].B.CHANNEL_VALUE = static_cast (fAdcChannel & 0xFFU);

Where CHANNEL_VALUE is:
union { /* Event Config 0..63 (Base+0x0030-0x012C) */
vuint32_t R;
struct {
vuint32_t :16;
vuint32_t TM:1;
vuint32_t CLR_FLAG:1;
vuint32_t :5;
vuint32_t ADC_SEL:1;
vuint32_t :1;
vuint32_t CHANNEL_VALUE:7;
} B;
} EVTCFGR[64];


Thanks in advance


Re: Resolve Rule 5-0-6: cast to 7 bits struct field - misra cpp - 31-10-2016

The way bit-fields should be handled is described on page 53 of MISRA C++ 2008

It is possible that the problem is caused by the "& 0xFFU" which gives a value in the range 0..255 (i.e. 8 bit) which is assigned to a 7 bit field

Try using & 0x7FU which gives a value in the range 0..127, i.e. 7 bit. It's also possible that the static_cast to a 32-bit type is the cause of the problem