public
Last active

getting bit value function inspired by duff's device

  • Download Gist
bitget.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#define bitGet(v, n) ((v >> n) & 1)
 
int bitGet(int v, int n){
switch(n - 1){
case 7: v >>= 1;
case 6: v >>= 1;
case 5: v >>= 1;
case 4: v >>= 1;
case 3: v >>= 1;
case 2: v >>= 1;
case 1: v >>= 1;
case 0: v >>= 1; break;
}
return v & 1;
}
 
int bitGet(int v, int n){
switch(n - 1){
case 1: v >>= 1;
case 0: v >>= 1; break;
default: n > 0 ? bitGet(v >> 1, n - 1) : bitGet( v >> 1, n + 1);
}
return v & 1;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.