嵌入式系统中结构体+冒号+数字的意义

来源:互联网 发布:s3c2440a数据手册 编辑:程序博客网 时间:2024/05/01 14:07


在最近嵌入式开发过程中经常遇到如下结构体形式:

/**@brief GATT Characteristic Properties. */typedef struct{  /* Standard properties */  uint8_t broadcast       :1; /**< Broadcasting of the value permitted. */  uint8_t read            :1; /**< Reading the value permitted. */  uint8_t write_wo_resp   :1; /**< Writing the value with Write Command permitted. */  uint8_t write           :1; /**< Writing the value with Write Request permitted. */  uint8_t notify          :1; /**< Notications of the value permitted. */  uint8_t indicate        :1; /**< Indications of the value permitted. */  uint8_t auth_signed_wr  :1; /**< Writing the value with Signed Write Command permitted. */} ble_gatt_char_props_t;/**@brief GATT Characteristic Extended Properties. */typedef struct{  /* Extended properties */  uint8_t reliable_wr     :1; /**< Writing the value with Queued Write operations permitted. */  uint8_t wr_aux          :1; /**< Writing the Characteristic User Description descriptor permitted. */} ble_gatt_char_ext_props_t;#endif // BLE_GATT_H__

后来发现 这种是位域操作的表示方法,也就是说后面加上“:1”的意思是这个成员的大小占所定义类型的1 bit,

“:2”占2 bit,依次类推。当然大小不能超过所定义类型包含的总bit数。

一个bytes(字节)是8 bit(bit)。例如你的结构中定义的类型是uint8_t,一个字节,共8bit,最大就不能超过8。


其实这样定义是为了节省空间。


例如上面的结构,定义的变量类型是uint8_t,是一字节类型,即8bit。
ble_gatt_char_props_t  总共占了7bit
因此结构的大小如果用sizeof(struct  ble_gatt_char_props_t  )计算,就是1bytes.(不满一个字节按一个字节算)


0 0
原创粉丝点击