0

i am having a structure as below which i am writing in a binary file in C

typedef struct abc{
unsigned int year       :12; 
unsigned int Month      :4;

unsigned int Date       :5;
unsigned int Hour       :5;
unsigned int Min        :6;

unsigned int Sec        :6;
unsigned int w          :1;
unsigned int x          :1; 
unsigned int y          :8;

unsigned int z          :8;

unsigned int q          :8;
}abc ;

i am filling the structure as follows:

abc.year=2020
abc.Month=2
abc.Date=31
abc.Hour=12
abc.Min=45
abc.Sec=25
abc.w=0
abc.x=1
abc.y=145
abc.z=100
abc.q=120

The output i am getting in the output file is as follows: E4 27 9F B5 99 91 64 78 The same thing i need to do with python..so please help me with this

Akshat Pant
  • 139
  • 12
  • 1
    Does this answer your question? [Does Python have a bitfield type?](https://stackoverflow.com/questions/142812/does-python-have-a-bitfield-type) – Robin Martijn Jun 09 '20 at 16:51
  • 1
    You have no guarantee at all where those bits wind up in the underlying `struct`. Just about everything about bit fields is implementation-defined so the `struct` layout can and will vary widely between different platforms and even different compilers on the same platform. If you need to know where the bits really are in a `struct`, you need to use bit masks and pay attention to platform endianness. See https://stackoverflow.com/questions/25345691/bit-fields-portability for more. – Andrew Henle Jun 09 '20 at 17:05
  • @AndrewHenle is there any way to replicate the above defined case in python? – Akshat Pant Jun 10 '20 at 04:01

0 Answers0