Why local variable s32 tmp is getting generated in code when bitwise OR operation is used in stateflow

1 次查看(过去 30 天)
When I use bitwise OR operation in stateflow of matlab model, signed 32 bit (s32) local variable "tmp" gets generated in the autogenerated code.
tmp = GetU8_CAN_St_Lidf_Ft_Motbk_2010() | lfc_U8_TEST_BIT4;
if (tmp > 255) {
tmp = 255;
}
SetU8_CAN_St_Lidf_Ft_Motbk_2010((T_u8)tmp)
But same is not the case if bitwise AND operation is used.
lfc_DW.u8_Temp_FT_Motbk = (T_u8)(GetU8_CAN_St_Lidf_Ft_Motbk_2010() &
lfc_U8_TEST_BIT4_OP);
Both "lfc_U8_TEST_BIT4" and "lfc_U8_TEST_BIT4_OP" are defined as Uint8 constant in stateflow.
Why such difference?

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-12
bitwise-or could increase the range of a value beyond 255. bitwise-and can never increase the range of a value.
The behaviour is like the "saturation" of uint8 values in MATLAB. If you have
X = uint8(250);
X = X + 10;
then the result in MATLAB is not 260, and it is not 5, it is 255 (just by definition in the language)

类别

Help CenterFile Exchange 中查找有关 Stateflow 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by