Skip to content

Instantly share code, notes, and snippets.

@hypeartist
Last active July 2, 2020 07:59
Show Gist options
  • Save hypeartist/f52dc664a7048860c92e38b48c1707a2 to your computer and use it in GitHub Desktop.
Save hypeartist/f52dc664a7048860c92e38b48c1707a2 to your computer and use it in GitHub Desktop.
Bitset
[StructLayout(LayoutKind.Sequential, Size = 256)]
public unsafe struct Bitset
{
public void Set(int bitIndex) => ((int*) Unsafe.AsPointer(ref Unsafe.AsRef(this)))[(bitIndex & ~7) >> 5] |= 1 << (bitIndex & 7);
public void Unset(int bitIndex) => ((int*)Unsafe.AsPointer(ref Unsafe.AsRef(this)))[(bitIndex & ~7) >> 5] ^= 1 << (bitIndex & 7);
public void SetAll() => Unsafe.InitBlock(Unsafe.AsPointer(ref Unsafe.AsRef(this)), 0xff, (uint) Unsafe.SizeOf<Bitset>());
public void UnsetAll() => Unsafe.InitBlock(Unsafe.AsPointer(ref Unsafe.AsRef(this)), 0x00, (uint) Unsafe.SizeOf<Bitset>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment