(file) Return to calversfunctions.c CVS log (file) (dir) Up to [Development] / JSOC / proj / globalhs / apps

 1 tplarson 1.3 char *cvsinfo = "cvsinfo: $Header: calversfunctions.c $";
 2              
 3 tplarson 1.1 /* 
 4              the following function is taken directly from chapter 2 of Kernighan and Ritchie
 5              getbits: get n bits from position p
 6              */
 7 tplarson 1.2 unsigned long long getbits(unsigned long long x, int p, int n)
 8 tplarson 1.1 {
 9 tplarson 1.2   return (x >> (p+1-n)) & ~(~0ull << n);
10 tplarson 1.1 }
11              
12              /*
13              Richard Heathfield's solution to exercise 2-6 of K&R 
14              */
15 tplarson 1.2 unsigned long long setbits(unsigned long long x, int p, int n, unsigned long long y)
16 tplarson 1.1 {
17 tplarson 1.2   return (x & ((~0ull << (p + 1)) | (~(~0ull << (p + 1 - n))))) | ((y & ~(~0ull << n)) << (p + 1 - n));
18              // another solution (untested):
19              //return (x & ~(~(~0<<n)<<(p+1)) | (y&~(~0<<n)) << (p+1));
20 tplarson 1.1 }
21              
22              unsigned long long fixcalver64(unsigned long long x)
23              {
24                int firstnibble = getbits(x,3,4);
25                if (firstnibble == 0)
26                  return setbits(x,3,4,2);
27                else
28                  return x;
29              }
30              
31              

Karen Tian
Powered by
ViewCVS 0.9.4