This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Representation of 24-bit and 40 bit values

Hi,

I am trying to develop models using the Specifications. I came across generic battery model which was using 24-bit values, time model which was using 15-bit and 40-bit values, scheduler model uses 4-bit and 76 bit values. 

In the stdint.h, there are representations for 8,16, 32 and 64 bit values.

For the 15-bit parameter, I can use 16-bit integer. Should I use the 8-bit integer for the 4-bit parameter then?

Also for the 24 bit, 40-bit and 76-bit, should I define the limits in the stdint.h or should I use the 32 bit integer for 24-bit, 64 bit integer for 40-bit?

Regards.

Parents
  • Hi Aryan

    You can't create variables with a different size than the basic types (8, 16, 32 or 64), but you can create structs that contain arbitrarily sized fields (up to 64 bits maximum) by using bit fields

    For the 76-bit value you mention this would look something like this:

    typedef struct __PACKED
    {
      uint32_t year : 7;
      uint32_t month : 12;
      uint32_t day : 5;
      uint32_t hour : 5;
      uint32_t minute : 6;
      uint32_t second : 6;
      uint32_t day_of_week : 7;
      uint32_t action : 4;
      uint32_t transition_time : 8;
      uint32_t scene_number : 16;
    } schedule_register_t;

    The __PACKED tag ensures that the struct is byte aligned, rather than 16 or 32 bit aligned, to keep the size in memory as small as possible. 

    Please be aware that the size of the struct will be 80 bits (10 bytes), and not 76, since the minimum alignment for any struct or variable is 1 byte. 

    Best regards
    Torbjørn

Reply
  • Hi Aryan

    You can't create variables with a different size than the basic types (8, 16, 32 or 64), but you can create structs that contain arbitrarily sized fields (up to 64 bits maximum) by using bit fields

    For the 76-bit value you mention this would look something like this:

    typedef struct __PACKED
    {
      uint32_t year : 7;
      uint32_t month : 12;
      uint32_t day : 5;
      uint32_t hour : 5;
      uint32_t minute : 6;
      uint32_t second : 6;
      uint32_t day_of_week : 7;
      uint32_t action : 4;
      uint32_t transition_time : 8;
      uint32_t scene_number : 16;
    } schedule_register_t;

    The __PACKED tag ensures that the struct is byte aligned, rather than 16 or 32 bit aligned, to keep the size in memory as small as possible. 

    Please be aware that the size of the struct will be 80 bits (10 bytes), and not 76, since the minimum alignment for any struct or variable is 1 byte. 

    Best regards
    Torbjørn

Children
Related