# bitfield Field

The **\<bitfield>** is a container field, which allows wrapped member fields to be serialized using limited number of bits (instead of bytes). The supported fields, that can be members of the **\<bitfield>**, are:

* [\<enum>](/commsdsl-specification/fields/enum.md)
* [\<int>](/commsdsl-specification/fields/int.md)
* [\<set>](/commsdsl-specification/fields/set.md)

Since **v2** of the specification it is also allowed to use [\<ref>](/commsdsl-specification/fields/ref.md) field, which references one of the field types above.

The **\<bitfield>** field has all the [common](/commsdsl-specification/fields/common.md) properties as well as extra properties and elements described below.

## Member Fields

Member fields need to be listed as children XML elements of the **\<bitfield>**. Every such member is expected to use **bitLength** property to specify its serialization length **in bits**. If it is not specified, then length in bits is calculated automatically as length **in bytes** multiplied by **8**.

```
<?xml version="1.0" encoding="UTF-8"?>
<schema name="MyProtocol" endian="big">
    <fields>
        <bitfield name="SomeBitfield">
            <int name="SomeIntMember" type="uint8" bitLength="3" />
            <set name="SomeSetMember" bitLength="3">
                ...
            </set>
            <enum name="SomeEnumMember" type="uint8" bitLength="2">
                ...
            </enum>
        </bitfield>
    </fields>
</schema>
```

**NOTE** that summary of all the lengths in bits of all the members must be divisible by **8** and mustn't exceed **64** bits, otherwise the code generator must report an error.

The members of **\<bitfield>** must be listed in order starting from the **least** significant bit. In the example above *SomeIntMember* occupies bits \[0 - 2], *SomeSetMember* occupies bits \[3 - 5], and *SomeEnumMember* occupies bits \[6 -7].

If there is any other [property](/commsdsl-specification/properties.md) defined as XML child of the **\<bitfield>**, then all the members must be wrapped in **\<members>** XML element for separation.

```
<?xml version="1.0" encoding="UTF-8"?>
<schema name="MyProtocol" endian="big">
    <fields>
        <bitfield name="SomeBitfield">
            <displayName value="Proper Bitfield Name" />
            <members>
                <int name="SomeIntMember" type="uint8" bitLength="3" />
                <set name="SomeSetMember" bitLength="3">
                    ...
                </set>
                <enum name="SomeEnumMember" type="uint8" bitLength="2">
                    ...
                </enum>
            </members>
        </bitfield>
    </fields>
</schema>
```

## Endian

When serializing, the **\<bitfield>** object needs to combine the values of all the members into single unsigned raw value of appropriate length, and write the received value using appropriate endian. By default **endian** of the [schema](/commsdsl-specification/schema.md) is used, unless it is overridden using extra **endian** property of the **\<bitfield>** field.

```
<?xml version="1.0" encoding="UTF-8"?>
<schema name="MyProtocol" endian="big">
    <fields>
        <bitfield name="SomeBitfield" endian="little">
            ...
        </bitfield>
    </fields>
</schema>
```

Use [properties table](/commsdsl-specification/appendix/bitfield.md) for future references.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alex-robenko.gitbook.io/commsdsl-specification/fields/bitfield.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
