Skip to content

Instantly share code, notes, and snippets.

@kinoko87
Last active September 23, 2022 22:57
Show Gist options
  • Select an option

  • Save kinoko87/c1499b1ffc1785ec03a17fb1c2ba55eb to your computer and use it in GitHub Desktop.

Select an option

Save kinoko87/c1499b1ffc1785ec03a17fb1c2ba55eb to your computer and use it in GitHub Desktop.
A abstract that allows you to use an integer as a bitfield.
abstract Bitfield(Int) from Int to Int
{
inline public function new(num:Int)
{
this = num;
}
public inline function setBit(n:Int)
{
this |= 0x1 << n;
}
public inline function clearBit(n:Int)
{
this &= ~(0x1 << n);
}
@:op([])
public inline function isBitSet(n:Int):Bool
{
return ((this >> n) & 0x1) == 1;
}
public inline function invertBits(n:Int)
{
this ^= n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment