# UnsafeBitArray

> An arbitrarily-sized array of bits.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Collections.LowLevel.Unsafe](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.ManagedKernelModule
* **Implements:** [INativeDisposable](/engine/6000.7/script-reference/unity/collections/inativedisposable.md), [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public struct UnsafeBitArray : INativeDisposable, IDisposable
```

## Remarks

The number of allocated bytes is always a multiple of 8. For example, a 65-bit array could fit in 9 bytes, but its allocation is actually 16 bytes.

## Fields

| Value                                                                                                      | Description                  |
| ---------------------------------------------------------------------------------------------------------- | ---------------------------- |
| [Allocator](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/allocator.md) | The allocator to use.        |
| [Capacity](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/capacity.md)   | The capacity number of bits. |
| [Length](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/length.md)       | The number of bits.          |
| [Ptr](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/ptr.md)             | Pointer to the data.         |

## Static Fields

| Value                                                                                                          | Description                                                     |
| -------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| [MaxCapacity](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/maxcapacity.md) | The maximum number of elements this type of container can hold. |

## Constructors

| Constructor                                                                                                                                                                                                                                                               | Description                                                                             |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| [UnsafeBitArray(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle,Unity.Collections.NativeArrayOptions)](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/ctor.md#unsafebitarray\(int-allocatorhandle-nativearrayoptions\)) | Initializes and returns an instance of UnsafeBitArray.                                  |
| [UnsafeBitArray(System.Void\*,System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/ctor.md#unsafebitarray\(void*-int-allocatorhandle\))                                     | Initializes and returns an instance of UnsafeBitArray which aliases an existing buffer. |

## Properties

| Property                                                                                                   | Description                                                      |
| ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| [IsCreated](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/iscreated.md) | Whether this array has been allocated (and not yet deallocated). |
| [IsEmpty](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/isempty.md)     | Whether the container is empty.                                  |

## Methods

| Method                                                                                                         | Description                                                                                                                                                                             |
| -------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AsReadOnly](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/asreadonly.md)   | Returns a readonly version of this UnsafeBitArray instance.                                                                                                                             |
| [Clear](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/clear.md)             | Sets all the bits to 0.                                                                                                                                                                 |
| [Copy](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/copy.md)               | Copies a range of bits from this array to another range in this array.                                                                                                                  |
| [CountBits](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/countbits.md)     | Returns the number of bits in a range that are 1.                                                                                                                                       |
| [Dispose](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/dispose.md)         | Releases all resources (memory and safety handles).                                                                                                                                     |
| [Find](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/find.md)               | Find a sequence of N contiguous 0 bits in a bit array. To achieve higher performance, it is not guaranteed that the found index is necessarily the first occurrence of such a sequence. |
| [GetBits](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/getbits.md)         | Returns a ulong which has bits copied from this array.                                                                                                                                  |
| [IsSet](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/isset.md)             | Returns true if the bit at an index is 1.                                                                                                                                               |
| [Resize](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/resize.md)           | Sets the length, expanding the capacity if necessary.                                                                                                                                   |
| [Set](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/set.md)                 | Sets the bit at an index to 0 or 1.                                                                                                                                                     |
| [SetBits](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/setbits.md)         | Sets a range of bits to 0 or 1.                                                                                                                                                         |
| [SetCapacity](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/setcapacity.md) | Sets the capacity.                                                                                                                                                                      |
| [TestAll](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/testall.md)         | Returns true if all of the bits in a range are 1.                                                                                                                                       |
| [TestAny](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/testany.md)         | Returns true if at least one of the bits in a range is 1.                                                                                                                               |
| [TestNone](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/testnone.md)       | Returns true if none of the bits in a range are 1 ("i.e." all bits in the range are 0).                                                                                                 |
| [TrimExcess](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/trimexcess.md)   | Sets the capacity to match what it would be if it had been originally initialized with all its entries.                                                                                 |

## Static Methods

| Method                                                                                         | Description                         |
| ---------------------------------------------------------------------------------------------- | ----------------------------------- |
| [Set](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/set.md) | Sets the bit at an index to 0 or 1. |

## Structs

| Struct                                                                                                                  | Description                                                                                   |
| ----------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| [UnsafeBitArray.ReadOnly](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafebitarray/readonly.md) | A read-only alias for the value of a UnsafeBitArray. Does not have its own allocated storage. |
