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

1 view (last 30 days)
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?

Answers (1)

Walter Roberson
Walter Roberson on 12 Feb 2016
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)

Categories

Find more on Stateflow in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!