1 /*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 4495754
27 * @summary Basic test for long bit twiddling
28 * @author Josh Bloch
29 * @key randomness
30 */
31
32 import java.util.Random;
33 import static java.lang.Long.*;
34
35 public class BitTwiddle {
36 private static final int N = 1000; // # of repetitions per test
37
38 public static void main(String args[]) {
39 Random rnd = new Random();
40
41 if (highestOneBit(0) != 0)
42 throw new RuntimeException("a");
43 if (highestOneBit(-1) != MIN_VALUE)
44 throw new RuntimeException("b");
45 if (highestOneBit(1) != 1)
46 throw new RuntimeException("c");
47
48 if (lowestOneBit(0) != 0)
49 throw new RuntimeException("d");
50 if (lowestOneBit(-1) != 1)
51 throw new RuntimeException("e");
52 if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
53 throw new RuntimeException("f");
54
55 for (int i = 0; i < N; i++) {
56 long x = rnd.nextLong();
57 if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
58 throw new RuntimeException("g: " + toHexString(x));
59 }
|
1 /*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @library /lib/testlibrary/
27 * @build jdk.testlibrary.*
28 * @run main BitTwiddle
29 * @bug 4495754 8078672
30 * @summary Basic test for long bit twiddling (use -Dseed=X to set PRNG seed)
31 * @author Josh Bloch
32 * @key randomness
33 */
34
35 import java.util.Random;
36 import static java.lang.Long.*;
37
38 public class BitTwiddle {
39 private static final int N = 1000; // # of repetitions per test
40
41 public static void main(String args[]) {
42 Random rnd = RandomFactory.getRandom();
43
44 if (highestOneBit(0) != 0)
45 throw new RuntimeException("a");
46 if (highestOneBit(-1) != MIN_VALUE)
47 throw new RuntimeException("b");
48 if (highestOneBit(1) != 1)
49 throw new RuntimeException("c");
50
51 if (lowestOneBit(0) != 0)
52 throw new RuntimeException("d");
53 if (lowestOneBit(-1) != 1)
54 throw new RuntimeException("e");
55 if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
56 throw new RuntimeException("f");
57
58 for (int i = 0; i < N; i++) {
59 long x = rnd.nextLong();
60 if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
61 throw new RuntimeException("g: " + toHexString(x));
62 }
|