version 1.1, 2012/09/24 20:05:43
|
version 1.2, 2013/01/24 06:41:12
|
|
|
the following function is taken directly from chapter 2 of Kernighan and Ritchie | the following function is taken directly from chapter 2 of Kernighan and Ritchie |
getbits: get n bits from position p | getbits: get n bits from position p |
*/ | */ |
unsigned getbits(unsigned x, int p, int n) |
unsigned long long getbits(unsigned long long x, int p, int n) |
{ | { |
return (x >> (p+1-n)) & ~(~0 << n); |
return (x >> (p+1-n)) & ~(~0ull << n); |
} | } |
| |
/* | /* |
Richard Heathfield's solution to exercise 2-6 of K&R | Richard Heathfield's solution to exercise 2-6 of K&R |
*/ | */ |
unsigned setbits(unsigned x, int p, int n, unsigned y) |
unsigned long long setbits(unsigned long long x, int p, int n, unsigned long long y) |
{ | { |
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n)); |
return (x & ((~0ull << (p + 1)) | (~(~0ull << (p + 1 - n))))) | ((y & ~(~0ull << n)) << (p + 1 - n)); |
// another solution (unchecked): |
// another solution (untested): |
//return (x & ~(~(~0<<n)<<p) | (y&~(~0<<n)) << p); |
//return (x & ~(~(~0<<n)<<(p+1)) | (y&~(~0<<n)) << (p+1)); |
} | } |
| |
unsigned long long fixcalver64(unsigned long long x) | unsigned long long fixcalver64(unsigned long long x) |