1826
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4495754
|
|
27 |
* @summary Basic test for long bit twiddling
|
|
28 |
* @author Josh Bloch
|
|
29 |
*
|
|
30 |
* @compile -source 1.5 BitTwiddle.java
|
|
31 |
* @run main BitTwiddle
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.util.Random;
|
|
35 |
import static java.lang.Long.*;
|
|
36 |
|
|
37 |
public class BitTwiddle {
|
|
38 |
private static final int N = 1000; // # of repetitions per test
|
|
39 |
|
|
40 |
public static void main(String args[]) {
|
|
41 |
Random rnd = new Random();
|
|
42 |
|
|
43 |
if (highestOneBit(0) != 0)
|
|
44 |
throw new RuntimeException("a");
|
|
45 |
if (highestOneBit(-1) != MIN_VALUE)
|
|
46 |
throw new RuntimeException("b");
|
|
47 |
if (highestOneBit(1) != 1)
|
|
48 |
throw new RuntimeException("c");
|
|
49 |
|
|
50 |
if (lowestOneBit(0) != 0)
|
|
51 |
throw new RuntimeException("d");
|
|
52 |
if (lowestOneBit(-1) != 1)
|
|
53 |
throw new RuntimeException("e");
|
|
54 |
if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
|
|
55 |
throw new RuntimeException("f");
|
|
56 |
|
|
57 |
for (int i = 0; i < N; i++) {
|
|
58 |
long x = rnd.nextLong();
|
|
59 |
if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
|
|
60 |
throw new RuntimeException("g: " + toHexString(x));
|
|
61 |
}
|
|
62 |
|
|
63 |
if (numberOfLeadingZeros(0) != SIZE)
|
|
64 |
throw new RuntimeException("h");
|
|
65 |
if (numberOfLeadingZeros(-1) != 0)
|
|
66 |
throw new RuntimeException("i");
|
|
67 |
if (numberOfLeadingZeros(1) != (SIZE - 1))
|
|
68 |
throw new RuntimeException("j");
|
|
69 |
|
|
70 |
if (numberOfTrailingZeros(0) != SIZE)
|
|
71 |
throw new RuntimeException("k");
|
|
72 |
if (numberOfTrailingZeros(1) != 0)
|
|
73 |
throw new RuntimeException("l");
|
|
74 |
if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
|
|
75 |
throw new RuntimeException("m");
|
|
76 |
|
|
77 |
for (int i = 0; i < N; i++) {
|
|
78 |
long x = rnd.nextLong();
|
|
79 |
if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
|
|
80 |
throw new RuntimeException("n: " + toHexString(x));
|
|
81 |
}
|
|
82 |
|
|
83 |
if (bitCount(0) != 0)
|
|
84 |
throw new RuntimeException("o");
|
|
85 |
|
|
86 |
for (int i = 0; i < SIZE; i++) {
|
|
87 |
long pow2 = 1L << i;
|
|
88 |
if (bitCount(pow2) != 1)
|
|
89 |
throw new RuntimeException("p: " + i);
|
|
90 |
if (bitCount(pow2 -1) != i)
|
|
91 |
throw new RuntimeException("q: " + i);
|
|
92 |
}
|
|
93 |
|
|
94 |
for (int i = 0; i < N; i++) {
|
|
95 |
long x = rnd.nextLong();
|
|
96 |
if (bitCount(x) != bitCount(reverse(x)))
|
|
97 |
throw new RuntimeException("r: " + toHexString(x));
|
|
98 |
}
|
|
99 |
|
|
100 |
for (int i = 0; i < N; i++) {
|
|
101 |
long x = rnd.nextLong();
|
|
102 |
int dist = rnd.nextInt();
|
|
103 |
if (bitCount(x) != bitCount(rotateRight(x, dist)))
|
|
104 |
throw new RuntimeException("s: " + toHexString(x) +
|
|
105 |
toHexString(dist));
|
|
106 |
if (bitCount(x) != bitCount(rotateLeft(x, dist)))
|
|
107 |
throw new RuntimeException("t: " + toHexString(x) +
|
|
108 |
toHexString(dist));
|
|
109 |
if (rotateRight(x, dist) != rotateLeft(x, -dist))
|
|
110 |
throw new RuntimeException("u: " + toHexString(x) +
|
|
111 |
toHexString(dist));
|
|
112 |
if (rotateRight(x, -dist) != rotateLeft(x, dist))
|
|
113 |
throw new RuntimeException("v: " + toHexString(x) +
|
|
114 |
toHexString(dist));
|
|
115 |
}
|
|
116 |
|
|
117 |
if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
|
|
118 |
|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
|
|
119 |
throw new RuntimeException("w");
|
|
120 |
|
|
121 |
for (int i = 0; i < N; i++) {
|
|
122 |
long x = rnd.nextLong();
|
|
123 |
int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
|
|
124 |
if (signum(x) != sign)
|
|
125 |
throw new RuntimeException("x: " + toHexString(x));
|
|
126 |
}
|
|
127 |
|
|
128 |
if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)
|
|
129 |
throw new RuntimeException("y");
|
|
130 |
|
|
131 |
for (int i = 0; i < N; i++) {
|
|
132 |
long x = rnd.nextLong();
|
|
133 |
if (bitCount(x) != bitCount(reverseBytes(x)))
|
|
134 |
throw new RuntimeException("z: " + toHexString(x));
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|