Inside the Union
, the device
member is stored using 8 bits.
However, ctypes
will automatically convert back and forth to native int
types for you, using (a good approximation of) the usual C rules for converting between types. For example, try this:
>>> x.device = 257
>>> x.device
1
So, you don't have anything to fix here.
However, you do have a problem:
I'm trying to make an 11-bit ID field for a CAN communication channel.
A Union
of an 8-bit int and a 3-bit int is 8 bits, just as in C. You want a Struct
. See Structures and unions and the type references linked from there for details.
On top of that, while a Struct
of a 3-bit int and an 8-bit int only has 11 bits of useful information, it will almost certainly take up at least 16 bits of memory, with 5 bits of padding.* You can only address memory a byte (8 bits) at a time. So… I'm not sure exactly what you were trying to do with this ID, but I suspect it's not going to work. If you explain your actual problem, ideally with some complete sample code, we can probably help.
* In fact, at least in current CPython versions, it will actually take up 32 bits of memory. See eryksun's comments for more details.