NHSE/NHSE.Core/Util/FlagUtil.cs
Kurt b88c518d5c
Update FieldItemEditor for 3.0.0 (#716)
Updates the Field Item Editor to render layers based on the entire map, and the per-patch positioning of each layer.
Import/export will gracefully handle upgrade/downgrade, and viewport import/export will gracefully update tiles rather than a per-acre basis.

Performance has also been slightly improved; no allocation is done anymore when updating the image.
2026-01-25 16:55:38 -06:00

39 lines
1.1 KiB
C#

using System;
namespace NHSE.Core;
/// <summary>
/// Utility logic for dealing with bitflags in a byte array.
/// </summary>
public static class FlagUtil
{
public static bool GetFlag(ReadOnlySpan<byte> arr, int offset, int bitIndex)
{
var b = arr[offset + (bitIndex >> 3)];
var mask = 1 << (bitIndex & 7);
return (b & mask) != 0;
}
public static void SetFlag(Span<byte> arr, int offset, int bitIndex, bool value)
{
offset += (bitIndex >> 3);
bitIndex &= 7; // ensure bit access is 0-7
arr[offset] &= (byte)~(1 << bitIndex);
arr[offset] |= (byte)((value ? 1 : 0) << bitIndex);
}
public static bool GetFlag(ReadOnlySpan<byte> arr, int bitIndex)
{
var b = arr[bitIndex >> 3];
var mask = 1 << (bitIndex & 7);
return (b & mask) != 0;
}
public static void SetFlag(Span<byte> arr, int bitIndex, bool value)
{
var offset = bitIndex >> 3;
bitIndex &= 7; // ensure bit access is 0-7
arr[offset] &= (byte)~(1 << bitIndex);
arr[offset] |= (byte)((value ? 1 : 0) << bitIndex);
}
}