簡體   English   中英

該代碼的含義是什么?

[英]What is the meaning of this code?

請幫助我理解此代碼的含義。 我第一次看到這種用法

typedef enum {

        E_1_DEFAULT = 0,
        E_1_1,
        E_1_2,
        E_1_3,
        E_1_4,
        E_1_5,
        E_1_255 = 255           //needs at least 8Bit

} APPLICATION_ENUM_1;                           

typedef enum {

        E_2_DEFAULT = 0,
        E_2_1,
        E_2_2,
        E_2_3                   //needs at least 2Bit

} APPLICATION_ENUM_2;                           

typedef enum {

        E_3_DEFAULT = 0,
        E_3_1,
        E_3_2,
        E_3_3,
        E_3_4,
        E_3_5,
        E_3_666 = 666           //needs at least 10Bit

} APPLICATION_ENUM_3;



typedef struct {

        APPLICATION_ENUM_3      var3:10;                // 10Bit
        APPLICATION_ENUM_1      var1:8;         // 18Bit
        APPLICATION_ENUM_2      var2:2;         // 20Bit
        uint8                   unnused_1:4;   // fill up the last whole byte -> 24Bit = 3byte

} APPLICATION_RAM;;

根據C ++標准

枚舉的基礎類型是整數類型,可以表示枚舉中定義的所有枚舉器值。

現在讓我們來看第一個定義

typedef enum {

        E_1_DEFAULT = 0,
        E_1_1,
        E_1_2,
        E_1_3,
        E_1_4,
        E_1_5,
        E_1_255 = 255           //needs at least 8Bit

} APPLICATION_ENUM_1; 

定義E_1_255 = 255確保此枚舉類型可以表示從0 to 255所有值,並且您至少需要8位來表示從0 to 255所有值

typedef struct {

        APPLICATION_ENUM_3      var3:10;                // 10Bit
        APPLICATION_ENUM_1      var1:8;         // 18Bit
        APPLICATION_ENUM_2      var2:2;         // 20Bit
        uint8                   unnused_1:4;   // 24Bit = 3byte

} APPLICATION_RAM;

上面的結構使用的是很少使用的位域結構。 基本上將APPLICATION_RAM聲明為具有

  • var3成員是10位
  • var1成員是8位
  • var2成員,即2位
  • 4位的unnused_1成員

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM