2 |
|
the following function is taken directly from chapter 2 of Kernighan and Ritchie |
3 |
|
getbits: get n bits from position p |
4 |
|
*/ |
5 |
< |
unsigned getbits(unsigned x, int p, int n) |
5 |
> |
unsigned long long getbits(unsigned long long x, int p, int n) |
6 |
|
{ |
7 |
< |
return (x >> (p+1-n)) & ~(~0 << n); |
7 |
> |
return (x >> (p+1-n)) & ~(~0ull << n); |
8 |
|
} |
9 |
|
|
10 |
|
/* |
11 |
|
Richard Heathfield's solution to exercise 2-6 of K&R |
12 |
|
*/ |
13 |
< |
unsigned setbits(unsigned x, int p, int n, unsigned y) |
13 |
> |
unsigned long long setbits(unsigned long long x, int p, int n, unsigned long long y) |
14 |
|
{ |
15 |
< |
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n)); |
16 |
< |
// another solution (unchecked): |
17 |
< |
//return (x & ~(~(~0<<n)<<p) | (y&~(~0<<n)) << p); |
15 |
> |
return (x & ((~0ull << (p + 1)) | (~(~0ull << (p + 1 - n))))) | ((y & ~(~0ull << n)) << (p + 1 - n)); |
16 |
> |
// another solution (untested): |
17 |
> |
//return (x & ~(~(~0<<n)<<(p+1)) | (y&~(~0<<n)) << (p+1)); |
18 |
|
} |
19 |
|
|
20 |
|
unsigned long long fixcalver64(unsigned long long x) |