summaryrefslogtreecommitdiffstats
path: root/tests/027-arithmetic/src/Main.java
blob: 4d0f74e7d14e1dfcc01425225874bc2c2d54d8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2006 The Android Open Source Project

/**
 * Test arithmetic operations.
 */
public class Main {

    static void shiftTest1()
    {
        final int[] mBytes = {
            0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb
        };
        long l;
        int i1, i2;

        i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24;
        i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24;
        l = i1 | ((long)i2 << 32);

	System.out.println("values are " + Integer.toHexString(i1)
	    + " and " + Integer.toHexString(i2));

        System.out.println("First l is " + Long.toHexString(l));

        l = (long)mBytes[0]
            | (long)mBytes[1] << 8
            | (long)mBytes[2] << 16
            | (long)mBytes[3] << 24
            | (long)mBytes[4] << 32
            | (long)mBytes[5] << 40
            | (long)mBytes[6] << 48
            | (long)mBytes[7] << 56;

        System.out.println("Second l is " + Long.toHexString(l));
    }

    static void shiftTest2()
    {
        long    a = 0x11;
        long    b = 0x22;
        long    c = 0x33;
        long    d = 0x44;
        long    e = 0x55;
        long    f = 0x66;
        long    g = 0x77;
        long    h = 0x88;

        long    result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
                         (e << 24) | (f << 16) | (g << 8) | h);

        System.out.println("shiftTest2 l is " + Long.toHexString(result));
    }

    static void convTest()
    {
        float f;
        double d;
        int i;
        long l;

        /* float --> int */
        f = 1234.5678f;
        i = (int) f;
        System.out.println("f=" + f + " --> i=" + i);

        f = -1234.5678f;
        i = (int) f;
        System.out.println("f=" + f + " --> i=" + i);

        /* double --> int */
        d = 1234.5678;
        i = (int) d;
        System.out.println("d=" + d + " --> i=" + i);

        d = -1234.5678;
        i = (int) d;
        System.out.println("d=" + d + " --> i=" + i);

        /* double --> long */
        d = 5678956789.0123;
        l = (long) d;
        System.out.println("d=" + d + " --> l=" + l);

        d = -5678956789.0123;
        l = (long) d;
        System.out.println("d=" + d + " --> l=" + l);

        /* int --> long */
        i = 7654;
        l = (long) i;
        System.out.println("i=" + i + " --> l=" + l);

        i = -7654;
        l = (long) i;
        System.out.println("i=" + i + " --> l=" + l);

        /* long --> int (with truncation) */
        l = 5678956789L;
        i = (int) l;
        System.out.println("l=" + l + " --> i=" + i);

        l = -5678956789L;
        i = (int) l;
        System.out.println("l=" + l + " --> i=" + i);

        /* int --> float */
        i = 1234;
        f = (float) i;
        System.out.println("i=" + i + " --> f=" + f);

        i = -1234;
        f = (float) i;
        System.out.println("i=" + i + " --> f=" + f);
    }

    static void unsignedShiftTest()
    {
        byte b = -4;
        short s = -4;
        char c = 0xfffc;
        int i = -4;

        b >>>= 4;
        s >>>= 4;
        c >>>= 4;
        i >>>= 4;

        System.out.println("b=" + b + ", s=" + s + ", c=" + (int)c + ", i=" +i);
        System.out.println("b=0x" + Integer.toHexString((int)b)
            + ", s=0x" + Integer.toHexString((int)s)
            + ", c=0x" + Integer.toHexString((int)c)
            + ", i=0x" + Integer.toHexString(i));
    }

    public static void main(String[] args) {
        convTest();
        shiftTest1();
        shiftTest2();
        unsignedShiftTest();
    }
}