using System;
namespace NexusForever.Shared.Network
{
public class NetworkBitArray
{
public enum BitOrder
{
LeastSignificantBit,
MostSignificantBit
}
private readonly byte[] buffer;
private readonly BitOrder order;
///
/// Initialise a new with supplied bit size.
///
public NetworkBitArray(uint size, BitOrder order)
{
buffer = new byte[(size - 1) / 8 + 1];
this.order = order;
}
///
/// Set bit at supplied position to value.
///
public void SetBit(uint position, bool value)
{
uint index = position / 8;
if (index > buffer.Length)
throw new ArgumentOutOfRangeException();
uint offset = order == BitOrder.LeastSignificantBit ? position % 8 : 7 - (position % 8);
buffer[index] |= (byte)((value ? 1u : 0u) << (int)offset);
}
///
/// Return underlying byte array.
///
public byte[] GetBuffer()
{
return buffer;
}
}
}