Created
June 11, 2014 02:43
-
-
Save cynx/2aa8c0e749f3ba7a9045 to your computer and use it in GitHub Desktop.
C function to extract n bits from p position of number x
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
#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