Skip to content

Commit 59fa6ae

Browse files
committed
[Python] Update bitfield documentation with better wording and another example
1 parent 4cdb2fe commit 59fa6ae

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/dev/annotation.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,35 @@ To create a bitfield in a structure, you can use the `bit_position` and `bit_wid
252252
```
253253

254254
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
255+
from the start of the structure, and the `bit_position` is the bit from the start of byte offset the member resides at, `bit_position` **cannot** be greater than `7`. The reason member
256256
offsets are byte offsets instead of bit offsets is historical, previous versions of Binary Ninja had no concept of bitwise
257257
structures.
258258

259+
For example, if you have a structure with the following members:
260+
261+
```c
262+
struct SmallFuncHeader __packed
263+
{
264+
uint32_t offset : 25;
265+
uint32_t paramCount : 7;
266+
uint32_t bytecodeSizeInBytes : 15;
267+
uint32_t functionName : 17;
268+
};
269+
```
270+
271+
This can be constructed in Python like so:
272+
273+
```pycon
274+
>>> t = TypeBuilder.structure(packed=True)
275+
... t.insert(0, Type.int(4, False), "offset", bit_position=0, bit_width=25)
276+
... t.insert(3, Type.int(4, False), "paramCount", bit_position=1, bit_width=7)
277+
... t.insert(4, Type.int(4, False), "bytecodeSizeInBytes", bit_position=0, bit_width=15)
278+
... t.insert(5, Type.int(4, False), "functionName", bit_position=7, bit_width=17)
279+
... t.members
280+
...
281+
[<uint32_t offset, offset 0x0, bit 0:25>, <uint32_t paramCount, offset 0x3, bit 1:7>, <uint32_t bytecodeSizeInBytes, offset 0x4, bit 0:15>, <uint32_t functionName, offset 0x5, bit 7:17>]
282+
```
283+
259284
#### Create Enumerations
260285

261286
```python

0 commit comments

Comments
 (0)