Debian Codegraph @

gov/fnal/eag/healpix/BitManipulation.java

raw
1 /*
2 * Created on Mar 15, 2005
3 *
4 */
5 package gov.fnal.eag.healpix;
6
7 /**
8 *
9 *
10 * bit manipulation class derived from Healpix
11 * fortran90 program.
12 *
13 * @author N Kuropatkin
14 */
15 public final class BitManipulation {
16 /**
17 * magic1 odd bits set constant
18 */
19 private static long magic1 = 89478485; // 101010101010101010101010101 LSB
20 /**
21 * magic2 - even bits set constant
22 */
23 private static long magic2 = 178956970; //1010101010101010101010101010 MSB
24
25
26 /**
27 * swaps low and high bits in the word i
28 * @param i integer input word
29 * @return int a word with odd and even bits interchanged
30 */
31 public static long swapLSBMSB(long i) {
32 long res = 0;
33 long lsb = (i & magic1);
34 long msb = (i & magic2);
35 res = msb/2 + lsb*2;
36 return res;
37 }
38 /**
39 * returns NOT i with even and odd bit positions interchanged
40 * @param i int input word
41 * @return int NOT (i with LSBMSB)
42 */
43 public static long invswapLSBMSB(long i) {
44 long res = 0;
45 long lsb = (i & magic1);
46 long msb = (i & magic2);
47 res = ~(msb/2+lsb*2);
48 return res;
49 }
50 /**
51 * returns i with even bits inverted
52 * @param i int input word
53 * @return int word with modified bits
54 */
55 public static long invLSB(long i) {
56 long res = 0;
57 res = (i ^ magic1); // returns exclusive OR with odd bits
58 return res;
59 }
60 /**
61 * returns i with odd bits inverted
62 * @param i int input word
63 * @return int word with modified bits
64 */
65 public static long invMSB(long i) {
66 long res = 0;
67 res = (i ^ magic2);
68 return res;
69 }
70 /**
71 * simulates behaviour of fortran90 MODULO function
72 * @param a double
73 * @param b double
74 * @return double MODULO
75 */
76 public static double MODULO(double a, double b) {
77 double res = 0.;
78 double c = 0;
79 long k = 0;
80 if (a>0.) {
81 if (b>0.) {
82 k = (long) (a/b);
83 res = a - k*b;
84 return res;
85 }
86 if (b<0.) {
87 k = (long)Math.rint(a/b);
88 res = a - k*b;
89 return res;
90 }
91 }
92 if (a<=0.) {
93 if (b<=0.) {
94 k = (long)(a/b);
95 res = a - k*b;
96 return res;
97 }
98 if (b>0.) {
99 k = (long)Math.rint(a/b);
100 res = a - k*b;
101 return res;
102 }
103 }
104 return res;
105 }
106
107 /**
108 * the main program for simple test
109 * @param pars
110 */
111 public static void main(String[] pars) {
112 // BitManipulation bm = new BitManipulation();
113 int l=0;
114 int k = 1;
115 long lsb;
116 long msb;
117 long mag1=0;;
118 long mag2=0;;
119 for (int i = 0; i<32; i++) {
120 mag1 += Math.pow(2.,l);
121 mag2 += Math.pow(2.,k);
122 System.out.println("l="+l+" mag1="+ mag1+" mag2="+mag2);
123 l +=2;
124 k +=2;
125 }
126 l = 0;
127 k = 1;
128 for (int i=0; i < 21; i++) {
129 lsb = (long)Math.pow(2.,l);
130 msb = (long)Math.pow(2.,k);
131 System.out.println(" l="+l+" 2^l="+lsb+" l="+k+" 2^l="+msb);
132 l +=2;
133 k +=2;
134
135 }
136 // BitManipulation bm = new BitManipulation();
137 double a= 6004765143422312.;
138 double b = BitManipulation.MODULO(a, 16.);
139 System.out.println("a="+a+" MODULO(1024)="+b);
140 }
141 }