2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Utility logic for dealing with bitflags in a byte array.
|
|
|
|
/// </summary>
|
|
|
|
public static class FlagUtil
|
2019-09-10 07:21:51 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Gets the requested <see cref="bitIndex"/> from the byte at <see cref="offset"/>.
|
2019-09-10 07:21:51 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="arr">Buffer to read</param>
|
|
|
|
/// <param name="offset">Offset of the byte</param>
|
|
|
|
/// <param name="bitIndex">Bit to read</param>
|
|
|
|
public static bool GetFlag(ReadOnlySpan<byte> arr, int offset, int bitIndex)
|
2019-09-10 07:21:51 +00:00
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
bitIndex &= 7; // ensure the bit access is 0-7
|
2022-06-18 18:04:24 +00:00
|
|
|
return ((arr[offset] >> bitIndex) & 1) != 0;
|
|
|
|
}
|
2019-09-10 07:21:51 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public static bool GetFlag(ReadOnlySpan<byte> arr, int index) => GetFlag(arr, index >> 3, index);
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the requested <see cref="bitIndex"/> value to the byte at <see cref="offset"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="arr">Buffer to modify</param>
|
|
|
|
/// <param name="offset">Offset of the byte</param>
|
|
|
|
/// <param name="bitIndex">Bit to write</param>
|
|
|
|
/// <param name="value">Bit flag value to set</param>
|
|
|
|
public static void SetFlag(Span<byte> arr, int offset, int bitIndex, bool value)
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
bitIndex &= 7; // ensure the bit access is 0-7
|
2022-06-18 18:04:24 +00:00
|
|
|
var current = arr[offset] & ~(1 << bitIndex);
|
|
|
|
var newValue = current | ((value ? 1 : 0) << bitIndex);
|
|
|
|
arr[offset] = (byte)newValue;
|
2019-09-10 07:21:51 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public static void SetFlag(Span<byte> arr, int index, bool value) => SetFlag(arr, index >> 3, index, value);
|
|
|
|
|
2023-12-28 08:11:56 +00:00
|
|
|
public static bool[] GetBitFlagArray(ReadOnlySpan<byte> data, int count)
|
2023-01-22 04:02:33 +00:00
|
|
|
{
|
|
|
|
var result = new bool[count];
|
2023-04-23 00:51:32 +00:00
|
|
|
GetBitFlagArray(data, result);
|
2023-01-22 04:02:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-23 00:51:32 +00:00
|
|
|
public static void GetBitFlagArray(ReadOnlySpan<byte> data, Span<bool> result)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
|
|
|
result[i] = (data[i >> 3] & (1 << (i & 7))) != 0;
|
|
|
|
}
|
|
|
|
|
2023-12-28 08:11:56 +00:00
|
|
|
public static bool[] GetBitFlagArray(ReadOnlySpan<byte> data) => GetBitFlagArray(data, data.Length << 3);
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
public static void SetBitFlagArray(Span<byte> data, ReadOnlySpan<bool> value)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < value.Length; i++)
|
|
|
|
{
|
|
|
|
var ofs = i >> 3;
|
|
|
|
var mask = (1 << (i & 7));
|
|
|
|
if (value[i])
|
|
|
|
data[ofs] |= (byte)mask;
|
|
|
|
else
|
|
|
|
data[ofs] &= (byte)~mask;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:32:25 +00:00
|
|
|
}
|