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

  • Thanks for the detailed explanation.

    For the other sizes like 15-bit; should I use 16-bit?

    for 24-bit; should I use 32-bit?

    for 40-bit; should I use 64-bit?  

    and specify the limits?

  • 15-bit; should I use 16-bit

    Well, you can't use less than 16.

    Being a 32-bit architecture, using 16 against 32 may not gain you much.

    for 24-bit; should I use 32-bit?

    That'll be easiest - but you could use 3 bytes, if you like.

    40-bit; should I use 64-bit

    Again, 64 will be easiest, but 5 bytes would do it.

    and specify the limits

    Not sure what you mean by that?

    should I define the limits in the stdint.h

    You should not fiddle with stdint.h.

    If you want custom types, then create a custom header...

Reply Children
Related