Understanding the interaction between bitfield and enumeration in S-function.

1 次查看(过去 30 天)
I have a manual code in which I have a structure with a bitfield with said enum's type:
typedef enum myEnum {
MY_ENUM_VALUE_0 = 0,
MY_ENUM_VALUE_1 = 1,
MY_ENUM_VALUE_2 = 2,
MY_ENUM_VALUE_3 = 3
} myEnum;
typedef struct myStructType {
myEnum enumVar :2;
unsigned char filling :6;
} myStructType;
I am using the legacy code tool to generate the S-function. This is my main function:
void mySFunctionMain (int* input, int* output)
{
myStructType instance;
instance.enumVar = *input;
*output = instance.enumVar;
}
I observe the following input => output behaviour:
input = 0 => output = 0
input = 1 => output = 1
input = 2 => output = -2
input = 3 => output = -1
Can you please explain to me why this is happening ? If the bad conversion happens at the first attribution:
instance.enumVar = *input;
Then it means the enum on 2 bits is somehow considered signed and the value is changed there -even though it doesn't change anything about the fact that the enumVar bitfield data for e.g. input = 3 would now "11". If the bad conversion happens at the second attribution:
*output = instance.enumVar;
Then it would mean the data bits are correct but the enum would be incorrectly interpreted as signed on 2 bits.

回答(0 个)

类别

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

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by