Skip to content

Instantly share code, notes, and snippets.

@AndrewIngram
Created February 2, 2022 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewIngram/f7184b580fd35193a9c2e0636fae8dd3 to your computer and use it in GitHub Desktop.
Save AndrewIngram/f7184b580fd35193a9c2e0636fae8dd3 to your computer and use it in GitHub Desktop.
Apply bitmask and shift
/*
* Given an input and a mask, will return just part we care about
*
* e.g.
* input: 0b11101111
* mask: 0b00110000
* returns: 0b10
*/
function maskedValue(input: number, mask: number): number {
return (input & mask) >> Math.log2(mask & -mask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment