Created
March 13, 2019 14:51
-
-
Save jonoliver82/6c9c1628f38f9a2ffde17fa446fd641d to your computer and use it in GitHub Desktop.
Extract value from ushort at bit offset and width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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