1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * Test arithmetic operations. 19 */ 20 public class Main { 21 shiftTest1()22 static void shiftTest1() 23 { 24 final int[] mBytes = { 25 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb 26 }; 27 long l; 28 int i1, i2; 29 30 i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24; 31 i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24; 32 l = i1 | ((long)i2 << 32); 33 34 System.out.println("values are " + Integer.toHexString(i1) 35 + " and " + Integer.toHexString(i2)); 36 37 System.out.println("First l is " + Long.toHexString(l)); 38 39 l = (long)mBytes[0] 40 | (long)mBytes[1] << 8 41 | (long)mBytes[2] << 16 42 | (long)mBytes[3] << 24 43 | (long)mBytes[4] << 32 44 | (long)mBytes[5] << 40 45 | (long)mBytes[6] << 48 46 | (long)mBytes[7] << 56; 47 48 System.out.println("Second l is " + Long.toHexString(l)); 49 } 50 shiftTest2()51 static void shiftTest2() 52 { 53 long a = 0x11; 54 long b = 0x22; 55 long c = 0x33; 56 long d = 0x44; 57 long e = 0x55; 58 long f = 0x66; 59 long g = 0x77; 60 long h = 0x88; 61 62 long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) | 63 (e << 24) | (f << 16) | (g << 8) | h); 64 65 System.out.println("shiftTest2 l is " + Long.toHexString(result)); 66 } 67 convTest()68 static void convTest() 69 { 70 float f; 71 double d; 72 int i; 73 long l; 74 75 /* float --> int */ 76 f = 1234.5678f; 77 i = (int) f; 78 System.out.println("f=" + f + " --> i=" + i); 79 80 f = -1234.5678f; 81 i = (int) f; 82 System.out.println("f=" + f + " --> i=" + i); 83 84 /* double --> int */ 85 d = 1234.5678; 86 i = (int) d; 87 System.out.println("d=" + d + " --> i=" + i); 88 89 d = -1234.5678; 90 i = (int) d; 91 System.out.println("d=" + d + " --> i=" + i); 92 93 /* double --> long */ 94 d = 5678956789.0123; 95 l = (long) d; 96 System.out.println("d=" + d + " --> l=" + l); 97 98 d = -5678956789.0123; 99 l = (long) d; 100 System.out.println("d=" + d + " --> l=" + l); 101 102 /* int --> long */ 103 i = 7654; 104 l = (long) i; 105 System.out.println("i=" + i + " --> l=" + l); 106 107 i = -7654; 108 l = (long) i; 109 System.out.println("i=" + i + " --> l=" + l); 110 111 /* long --> int (with truncation) */ 112 l = 5678956789L; 113 i = (int) l; 114 System.out.println("l=" + l + " --> i=" + i); 115 116 l = -5678956789L; 117 i = (int) l; 118 System.out.println("l=" + l + " --> i=" + i); 119 120 /* int --> float */ 121 i = 1234; 122 f = (float) i; 123 System.out.println("i=" + i + " --> f=" + f); 124 125 i = -1234; 126 f = (float) i; 127 System.out.println("i=" + i + " --> f=" + f); 128 } 129 unsignedShiftTest()130 static void unsignedShiftTest() 131 { 132 byte b = -4; 133 short s = -4; 134 char c = 0xfffc; 135 int i = -4; 136 137 b >>>= 4; 138 s >>>= 4; 139 c >>>= 4; 140 i >>>= 4; 141 142 System.out.println("b=" + b + ", s=" + s + ", c=" + (int)c + ", i=" +i); 143 System.out.println("b=0x" + Integer.toHexString((int)b) 144 + ", s=0x" + Integer.toHexString((int)s) 145 + ", c=0x" + Integer.toHexString((int)c) 146 + ", i=0x" + Integer.toHexString(i)); 147 } 148 main(String[] args)149 public static void main(String[] args) { 150 convTest(); 151 shiftTest1(); 152 shiftTest2(); 153 unsignedShiftTest(); 154 } 155 } 156