Skip to content

Instantly share code, notes, and snippets.

@jonoliver82
Created March 13, 2019 14:51
Show Gist options
  • Save jonoliver82/6c9c1628f38f9a2ffde17fa446fd641d to your computer and use it in GitHub Desktop.
Save jonoliver82/6c9c1628f38f9a2ffde17fa446fd641d to your computer and use it in GitHub Desktop.
Extract value from ushort at bit offset and width
public static class UShortExtensions
{
public static int Section(this ushort value, int offset, int width)
{
var mask = 0;
for (int i = 0; i < width; i++)
{
mask |= 1 << i;
}
var result = (value >> offset) & mask;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment