# ctypes.Structure and bitfields

**URL:** https://discuss.python.org/t/ctypes-structure-and-bitfields/25029
**Category:** Python Help
**Created:** [March 20, 2023, 3:20pm UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029 "2023-03-20T15:20:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DakshKK](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dakshkk/32/11643_2.png) [@DakshKK](https://discuss.python.org/u/DakshKK)
#### Post date: [March 20, 2023, 3:20pm UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/1 "2023-03-20T15:20:39Z")

</div>

I have been using bit-fields with `ctypes.Structure`.

I have a bitfield struct, and my issue is that no matter what I do, I am unable to set the values for my fields.

A structure with the following definition:

```python
from ctypes import Structure, c_byte, c_int
class Demo (Structure):
 _fields_ = [
  ('f1', c_byte, 8),
  ('f2', c_int, 21),
  ('f3', c_byte, 2),
  ('f4', c_byte, 1)
 ]

```

I have set the above struct using `from_buffer_copy` with a value of `b'\x01\x0f\x00\xe0'` (Binary representation of C struct, with the underneath values).

This should set the following values

- `f1`: 1
- `f2`: 15
- `f3`: -1 (3, overflow makes it -1)
- `f4`: -1 (1, overflow makes it -1)

Manual setting, or `from_buffer_copy`, achieve nothing; `f3`, and `f4` remain 0.

Could someone help me set `f3`, and `f4` to corresponding values (-1 in this example). Also how would I access these fields in bit form of their corresponding size.

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 10:59pm UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/2 "2023-03-20T22:59:19Z")

</div>

Hi,  
Hot take - maybe try with setting _pack_ attribute to 1 for this class?

[SO CTypes Bitfields](https://stackoverflow.com/questions/20380451/python-ctypes-bitfields) can be used as nice reference, eventually [This project](https://github.com/NJDFan/ctypes-bitfield)

---

<div class="post-metadata">

### Author: ![DakshKK](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dakshkk/32/11643_2.png) [@DakshKK](https://discuss.python.org/u/DakshKK)
#### Post date: [March 20, 2023, 11:47pm UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/3 "2023-03-20T23:47:56Z")

</div>

Setting `_pack_` does not achieve any changes in the code.

You could check out [SO `ctypes.Structure` and bit fields](https://stackoverflow.com/q/75788066). Also this [issue](https://github.com/python/cpython/issues/102844), now seems to provide an answer to the error.

This is an interpreter problem, because of a fault in the Python implementation for `ctypes.Structure`.

---

<div class="post-metadata">

### Author: ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)
#### Post date: [March 21, 2023, 12:19am UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/4 "2023-03-21T00:19:56Z")

</div>

> [@DakshKK](#):
>
> This is an interpreter problem, because of a fault in the Python implementation for `ctypes.Structure`.

General tip: Don’t be too hasty to claim that something’s a bug. It just makes you look like a fool if you’re wrong.

In this case, I tweaked your code like this:

```auto
from ctypes import Structure, c_int
class Demo(Structure):
	_fields_ = [
		('f1', c_int, 8),
		('f2', c_int, 21),
		('f3', c_int, 2),
		('f4', c_int, 1),
	]

d = Demo.from_buffer_copy(b'\x01\x0f\x00\xe0')
print(d.f1, d.f2, d.f3, d.f4)

```

and I got values of 1, 15, -1, and -1. The only difference is that I looked at the documentation, rather than going to a toxic Q&A site.

> **[ctypes — A foreign function library for Python](https://docs.python.org/3/library/ctypes.html#bit-fields-in-structures-and-unions)**
>
> Source code: Lib/ctypes ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these ...

and then used the `c_int` type as is documented.

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 21, 2023, 6:14am UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/5 "2023-03-21T06:14:08Z")

</div>

So it seems that we still need to find a way to get bitfields working right. I also don’t assume bug in core python module, so we’re on the same page here.  
I will try to search my resources in order to find bitfield related ctypes structures to show, because we deal with vxlapi.dll 🙂

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 22, 2023, 8:55am UTC](https://discuss.python.org/t/ctypes-structure-and-bitfields/25029/6 "2023-03-22T08:55:10Z")

</div>

Okay, I found something.  
[ctypes-bitfield](https://github.com/NJDFan/ctypes-bitfield/blob/master/bitfield/ __init__.py) uses pack attribute and few other things to achieve bitfields with ctypes. Plus, here’s more sophisticated usage of ctypes, for Windows: [pyWinAPI](https://github.com/kdschlosser/pyWinAPI). I hope that helps! 🙂
