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

1 回表示 (過去 30 日間)
Franck Yin
Franck Yin 2018 年 6 月 15 日
編集済み: Franck Yin 2018 年 6 月 20 日
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 件)

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by