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?

6 次查看(过去 30 天)
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]

回答(1 个)

Alexandre De Barros
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!

Translated by