Skip to content

Instantly share code, notes, and snippets.

@cynx
Created June 11, 2014 02:43
Show Gist options
  • Save cynx/2aa8c0e749f3ba7a9045 to your computer and use it in GitHub Desktop.
Save cynx/2aa8c0e749f3ba7a9045 to your computer and use it in GitHub Desktop.
C function to extract n bits from p position of number x
#include <stdio.h>
unsigned getbits(unsigned x,int p,int n);
int main()
{
printf("%d",getbits(5, 4, 3));
return 0;
}
unsigned getbits(unsigned x,int p,int n)
{
return (x >> (p + 1 - n) & ~(~0 << n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment