Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Long/BitTwiddle.java
38813 views
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
}
60
61
if (numberOfLeadingZeros(0) != SIZE)
62
throw new RuntimeException("h");
63
if (numberOfLeadingZeros(-1) != 0)
64
throw new RuntimeException("i");
65
if (numberOfLeadingZeros(1) != (SIZE - 1))
66
throw new RuntimeException("j");
67
68
if (numberOfTrailingZeros(0) != SIZE)
69
throw new RuntimeException("k");
70
if (numberOfTrailingZeros(1) != 0)
71
throw new RuntimeException("l");
72
if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
73
throw new RuntimeException("m");
74
75
for (int i = 0; i < N; i++) {
76
long x = rnd.nextLong();
77
if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
78
throw new RuntimeException("n: " + toHexString(x));
79
}
80
81
if (bitCount(0) != 0)
82
throw new RuntimeException("o");
83
84
for (int i = 0; i < SIZE; i++) {
85
long pow2 = 1L << i;
86
if (bitCount(pow2) != 1)
87
throw new RuntimeException("p: " + i);
88
if (bitCount(pow2 -1) != i)
89
throw new RuntimeException("q: " + i);
90
}
91
92
for (int i = 0; i < N; i++) {
93
long x = rnd.nextLong();
94
if (bitCount(x) != bitCount(reverse(x)))
95
throw new RuntimeException("r: " + toHexString(x));
96
}
97
98
for (int i = 0; i < N; i++) {
99
long x = rnd.nextLong();
100
int dist = rnd.nextInt();
101
if (bitCount(x) != bitCount(rotateRight(x, dist)))
102
throw new RuntimeException("s: " + toHexString(x) +
103
toHexString(dist));
104
if (bitCount(x) != bitCount(rotateLeft(x, dist)))
105
throw new RuntimeException("t: " + toHexString(x) +
106
toHexString(dist));
107
if (rotateRight(x, dist) != rotateLeft(x, -dist))
108
throw new RuntimeException("u: " + toHexString(x) +
109
toHexString(dist));
110
if (rotateRight(x, -dist) != rotateLeft(x, dist))
111
throw new RuntimeException("v: " + toHexString(x) +
112
toHexString(dist));
113
}
114
115
if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
116
|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
117
throw new RuntimeException("w");
118
119
for (int i = 0; i < N; i++) {
120
long x = rnd.nextLong();
121
int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
122
if (signum(x) != sign)
123
throw new RuntimeException("x: " + toHexString(x));
124
}
125
126
if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)
127
throw new RuntimeException("y");
128
129
for (int i = 0; i < N; i++) {
130
long x = rnd.nextLong();
131
if (bitCount(x) != bitCount(reverseBytes(x)))
132
throw new RuntimeException("z: " + toHexString(x));
133
}
134
}
135
}
136
137