Struct NativeBitArray
An arbitrarily-sized array of bits.
Namespace: Unity.Collections
Syntax
public struct NativeBitArray : 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.
Constructors
NativeBitArray(Int32, AllocatorManager.AllocatorHandle, NativeArrayOptions)
Initializes and returns an instance of NativeBitArray.
Declaration
public NativeBitArray(int numBits, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numBits | The number of bits. |
| AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
| NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. |
Properties
IsCreated
Whether this array has been allocated (and not yet deallocated).
Declaration
public bool IsCreated { get; }
Property Value
| Type | Description |
|---|---|
| Boolean | True if this array has been allocated (and not yet deallocated). |
Length
Returns the number of bits.
Declaration
public int Length { get; }
Property Value
| Type | Description |
|---|---|
| Int32 | The number of bits. |
Methods
AsNativeArray<T>()
Returns a native array that aliases the content of this array.
Declaration
public NativeArray<T> AsNativeArray<T>()
where T : struct
Returns
| Type | Description |
|---|---|
| NativeArray<T> | A native array that aliases the content of this array. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the aliased array. |
Clear()
Sets all the bits to 0.
Declaration
public void Clear()
Copy(Int32, Int32, Int32)
Copies a range of bits from this array to another range in this array.
Declaration
public void Copy(int dstPos, int srcPos, int numBits)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dstPos | Index of the first bit to set. |
| Int32 | srcPos | Index of the first bit to copy. |
| Int32 | numBits | Number of bits to copy. |
Remarks
The bits to copy run from index srcPos up to (but not including) srcPos + numBits.
The bits to set run from index dstPos up to (but not including) dstPos + numBits.
The ranges may overlap, but the result in the overlapping region is undefined.
Copy(Int32, ref NativeBitArray, Int32, Int32)
Copies a range of bits from an array to a range of bits in this array.
Declaration
public void Copy(int dstPos, ref NativeBitArray srcBitArray, int srcPos, int numBits)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dstPos | Index of the first bit to set. |
| NativeBitArray | srcBitArray | The source array. |
| Int32 | srcPos | Index of the first bit to copy. |
| Int32 | numBits | The number of bits to copy. |
Remarks
The bits to copy in the source array run from index srcPos up to (but not including) srcPos + numBits.
The bits to set in the destination array run from index dstPos up to (but not including) dstPos + numBits.
When the source and destination are the same array, the ranges may still overlap, but the result in the overlapping region is undefined.
CountBits(Int32, Int32)
Returns the number of bits in a range that are 1.
Declaration
public int CountBits(int pos, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit at which to start searching. |
| Int32 | numBits | Number of bits to test. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Int32 | The number of bits in a range of bits that are 1. |
Dispose()
Releases all resources (memory and safety handles).
Declaration
public void Dispose()
Dispose(JobHandle)
Creates and schedules a job that will dispose this array.
Declaration
public JobHandle Dispose(JobHandle inputDeps)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | inputDeps | The handle of a job which the new job will depend upon. |
Returns
| Type | Description |
|---|---|
| JobHandle | The handle of a new job that will dispose this array. The new job depends upon inputDeps. |
Implements
Find(Int32, Int32)
Finds the first length-N contiguous sequence of 0 bits in this bit array.
Declaration
public int Find(int pos, int numBits)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index at which to start searching. |
| Int32 | numBits | Number of contiguous 0 bits to look for. |
Returns
| Type | Description |
|---|---|
| Int32 | The index in this array where the sequence is found. The index will be greater than or equal to |
Find(Int32, Int32, Int32)
Finds the first length-N contiguous sequence of 0 bits in this bit array. Searches only a subsection.
Declaration
public int Find(int pos, int count, int numBits)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index at which to start searching. |
| Int32 | count | Number of bits to search. |
| Int32 | numBits | Number of contiguous 0 bits to look for. |
Returns
| Type | Description |
|---|---|
| Int32 | The index in this array where the sequence is found. The index will be greater than or equal to |
GetBits(Int32, Int32)
Returns a ulong which has bits copied from this array.
Declaration
public ulong GetBits(int pos, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the first bit to get. |
| Int32 | numBits | Number of bits to get (must be between 1 and 64). |
Returns
| Type | Description |
|---|---|
| UInt64 | A ulong which has bits copied from this array. |
Remarks
The source bits in this array run from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
The first source bit is copied to the lowest bit of the ulong; the second source bit is copied to the second-lowest bit of the ulong; and so forth. Any remaining bits in the ulong will be 0.
IsSet(Int32)
Returns true if the bit at an index is 1.
Declaration
public bool IsSet(int pos)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit to test. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the bit at the index is 1. |
Set(Int32, Boolean)
Sets the bit at an index to 0 or 1.
Declaration
public void Set(int pos, bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit to set. |
| Boolean | value | True for 1, false for 0. |
SetBits(Int32, Boolean, Int32)
Sets a range of bits to 0 or 1.
Declaration
public void SetBits(int pos, bool value, int numBits)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the first bit to set. |
| Boolean | value | True for 1, false for 0. |
| Int32 | numBits | Number of bits to set. |
Remarks
The range runs from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
SetBits(Int32, UInt64, Int32)
Copies bits of a ulong to bits in this array.
Declaration
public void SetBits(int pos, ulong value, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the first bit to set. |
| UInt64 | value | Unsigned long from which to copy bits. |
| Int32 | numBits | Number of bits to set (must be between 1 and 64). |
Remarks
The destination bits in this array run from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
The lowest bit of the ulong is copied to the first destination bit; the second-lowest bit of the ulong is copied to the second destination bit; and so forth.
TestAll(Int32, Int32)
Returns true if all of the bits in a range are 1.
Declaration
public bool TestAll(int pos, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit at which to start searching. |
| Int32 | numBits | Number of bits to test. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Boolean | True if all of the bits in range |
TestAny(Int32, Int32)
Returns true if at least one of the bits in a range is 1.
Declaration
public bool TestAny(int pos, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit at which to start searching. |
| Int32 | numBits | Number of bits to test. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Boolean | True if one ore more of the bits in range |
TestNone(Int32, Int32)
Returns true if none of the bits in a range are 1 (i.e. all bits in the range are 0).
Declaration
public bool TestNone(int pos, int numBits = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | pos | Index of the bit at which to start searching. |
| Int32 | numBits | Number of bits to test. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Boolean | Returns true if none of the bits in range |