2022-01-03 05:35:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Utility logic for dealing with bitflags in a byte array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class FlagUtil
|
|
|
|
|
{
|
2021-05-15 18:32:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the requested <see cref="bitIndex"/> from the byte at <see cref="offset"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="arr">Buffer to read</param>
|
|
|
|
|
/// <param name="offset">Offset of the byte</param>
|
|
|
|
|
/// <param name="bitIndex">Bit to read</param>
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static bool GetFlag(ReadOnlySpan<byte> arr, int offset, int bitIndex)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
|
|
|
|
bitIndex &= 7; // ensure bit access is 0-7
|
|
|
|
|
return (arr[offset] >> bitIndex & 1) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 18:32:25 +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>
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static void SetFlag(Span<byte> arr, int offset, int bitIndex, bool value)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
|
|
|
|
bitIndex &= 7; // ensure bit access is 0-7
|
2021-05-15 18:32:25 +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
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-15 18:32:25 +00:00
|
|
|
|
}
|