Skip to content

Instantly share code, notes, and snippets.

@sriramster
Last active August 29, 2015 14:02
Show Gist options
  • Save sriramster/13cb5b4fd5615e01b858 to your computer and use it in GitHub Desktop.
Save sriramster/13cb5b4fd5615e01b858 to your computer and use it in GitHub Desktop.
bit extraction
#include <stdio.h>
#define bit_ext(val, m, n) \
int k = 1 << (m-1); \
k = k | (k -1); \
k = k ^ (1 << (n + 1)); \
int j = 1 << (n + 1); \
j = j ^ (j - 1); \
k = k ^ j; \
val = val & k; \
val = val >> n; \
int main()
{
int i = 0xffff;
bit_ext(i, 7, 4)
printf("\n %d",i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment