Skip to content

Commit acc0d52

Browse files
committed
Update developer documentation for creating bitfields
1 parent fa75724 commit acc0d52

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/dev/annotation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ StructureType.create(members=[(Type.int(4), 'field_0'), (Type.int(4), 'field_4')
238238
StructureType.create(members=[(Type.int(4), 'field_0')], type=StructureVariant.ClassStructureType)
239239
```
240240

241+
#### Create Bitfields in a Structure
242+
243+
To create a bitfield in a structure, you can use the `bit_position` and `bit_width` parameters when inserting a member at an offset.
244+
245+
```pycon
246+
>>> t = TypeBuilder.structure()
247+
>>> t.insert(0, Type.int(4), "field_0")
248+
>>> t.insert(4, Type.int(4), "bitfield_1", bit_width=4)
249+
>>> t.insert(4, Type.int(4), "bitfield_2", bit_position=4, bit_width=4)
250+
>>> t.members
251+
[<int32_t field_0, offset 0x0>, <int32_t bitfield_1, offset 0x4, bit 0:4>, <int32_t bitfield_2, offset 0x4, bit 4:4>]
252+
```
253+
254+
It is important to note the distinction between the `bit_position` and `offset` parameters. The `offset` is a byte offset
255+
from the start of the structure, and the `bit_position` is a bit offset from the start of byte offset. The reason member
256+
offsets are byte offsets instead of bit offsets is historical, previous versions of Binary Ninja had no concept of bitwise
257+
structures.
258+
241259
#### Create Enumerations
242260

243261
```python

0 commit comments

Comments
 (0)