Pplyspace complain about conversion overflow: Conversion from int 32 to unsigned int 32. However everything I have it defined as unsigned. Is it something in ~ operator?

7 views (last 30 days)
typedef union _EXIPC_TRACK_T {
U16 U;
struct _EXIPC_XTRACK_T {
U16 ct_tm :8;
U16 ct_fm :4;
U16 ct_nt :4;
} X;
} EXIPC_TRACK_T;
auto EXIPC_TRACK_T au_track0, au_track1;
(*p_MsgGrp->p_d_trk_ds)[0] = (U16) (( au_track0.U) & 0xFFFFU);
(*p_MsgGrp->p_d_trk_ds)[1] = (U16) ((~au_track0.U) & 0xFFFFU);
[SL: edited to apply code formatting]

Answers (1)

Alexandre De Barros
Alexandre De Barros on 2 Nov 2017
Hello Ahmad,
It has something to do with the ~ operator indeed. This operator will not be performed on U16 but on the "int" type. There is an "integral promotion" taking place here.
You will find more information on integral promotion for example here: https://www.securecoding.cert.org/confluence/display/c/INT02-C.+Understand+integer+conversion+rules
If you want to get rid of the overflow, you can explicitely cast the U16 to unsigned int before doing the negation:
u = (U16) ((~(unsigned int)au_track0.U) & 0xFFFFU);
Best regards,
Alexandre

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!