• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006 The Android Open Source Project
2 
3 /**
4  * Test arithmetic operations.
5  */
6 public class Main {
7 
shiftTest1()8     static void shiftTest1()
9     {
10         final int[] mBytes = {
11             0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb
12         };
13         long l;
14         int i1, i2;
15 
16         i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24;
17         i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24;
18         l = i1 | ((long)i2 << 32);
19 
20 	System.out.println("values are " + Integer.toHexString(i1)
21 	    + " and " + Integer.toHexString(i2));
22 
23         System.out.println("First l is " + Long.toHexString(l));
24 
25         l = (long)mBytes[0]
26             | (long)mBytes[1] << 8
27             | (long)mBytes[2] << 16
28             | (long)mBytes[3] << 24
29             | (long)mBytes[4] << 32
30             | (long)mBytes[5] << 40
31             | (long)mBytes[6] << 48
32             | (long)mBytes[7] << 56;
33 
34         System.out.println("Second l is " + Long.toHexString(l));
35     }
36 
shiftTest2()37     static void shiftTest2()
38     {
39         long    a = 0x11;
40         long    b = 0x22;
41         long    c = 0x33;
42         long    d = 0x44;
43         long    e = 0x55;
44         long    f = 0x66;
45         long    g = 0x77;
46         long    h = 0x88;
47 
48         long    result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
49                          (e << 24) | (f << 16) | (g << 8) | h);
50 
51         System.out.println("shiftTest2 l is " + Long.toHexString(result));
52     }
53 
convTest()54     static void convTest()
55     {
56         float f;
57         double d;
58         int i;
59         long l;
60 
61         /* float --> int */
62         f = 1234.5678f;
63         i = (int) f;
64         System.out.println("f=" + f + " --> i=" + i);
65 
66         f = -1234.5678f;
67         i = (int) f;
68         System.out.println("f=" + f + " --> i=" + i);
69 
70         /* double --> int */
71         d = 1234.5678;
72         i = (int) d;
73         System.out.println("d=" + d + " --> i=" + i);
74 
75         d = -1234.5678;
76         i = (int) d;
77         System.out.println("d=" + d + " --> i=" + i);
78 
79         /* double --> long */
80         d = 5678956789.0123;
81         l = (long) d;
82         System.out.println("d=" + d + " --> l=" + l);
83 
84         d = -5678956789.0123;
85         l = (long) d;
86         System.out.println("d=" + d + " --> l=" + l);
87 
88         /* int --> long */
89         i = 7654;
90         l = (long) i;
91         System.out.println("i=" + i + " --> l=" + l);
92 
93         i = -7654;
94         l = (long) i;
95         System.out.println("i=" + i + " --> l=" + l);
96 
97         /* long --> int (with truncation) */
98         l = 5678956789L;
99         i = (int) l;
100         System.out.println("l=" + l + " --> i=" + i);
101 
102         l = -5678956789L;
103         i = (int) l;
104         System.out.println("l=" + l + " --> i=" + i);
105 
106         /* int --> float */
107         i = 1234;
108         f = (float) i;
109         System.out.println("i=" + i + " --> f=" + f);
110 
111         i = -1234;
112         f = (float) i;
113         System.out.println("i=" + i + " --> f=" + f);
114     }
115 
unsignedShiftTest()116     static void unsignedShiftTest()
117     {
118         byte b = -4;
119         short s = -4;
120         char c = 0xfffc;
121         int i = -4;
122 
123         b >>>= 4;
124         s >>>= 4;
125         c >>>= 4;
126         i >>>= 4;
127 
128         System.out.println("b=" + b + ", s=" + s + ", c=" + (int)c + ", i=" +i);
129         System.out.println("b=0x" + Integer.toHexString((int)b)
130             + ", s=0x" + Integer.toHexString((int)s)
131             + ", c=0x" + Integer.toHexString((int)c)
132             + ", i=0x" + Integer.toHexString(i));
133     }
134 
main(String[] args)135     public static void main(String[] args) {
136         convTest();
137         shiftTest1();
138         shiftTest2();
139         unsignedShiftTest();
140     }
141 }
142 
143