/* * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @library /test/lib * @build jdk.test.lib.RandomFactory * @run main BitTwiddle * @bug 4495754 8078672 * @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed) * @author Josh Bloch * @key randomness
*/
for (int i = 0; i < N; i++) { int x = rnd.nextInt(); if (highestOneBit(x) != reverse(lowestOneBit(reverse(x)))) thrownew RuntimeException("g: " + toHexString(x));
}
if (numberOfLeadingZeros(0) != SIZE) thrownew RuntimeException("h"); if (numberOfLeadingZeros(-1) != 0) thrownew RuntimeException("i"); if (numberOfLeadingZeros(1) != (SIZE - 1)) thrownew RuntimeException("j"); if (numberOfLeadingZeros(Integer.MAX_VALUE) != 1) thrownew RuntimeException("lzmax");
if (numberOfTrailingZeros(0) != SIZE) thrownew RuntimeException("k"); if (numberOfTrailingZeros(1) != 0) thrownew RuntimeException("l"); if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1)) thrownew RuntimeException("m");
for (int i = 0; i < N; i++) { int x = rnd.nextInt(); if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x))) thrownew RuntimeException("n: " + toHexString(x));
} for (int i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) { if (numberOfLeadingZeros(i) != r ||
numberOfTrailingZeros(i) != (SIZE - r - 1) ||
numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) { thrownew RuntimeException("lzx: " + toHexString(i));
}
}
if (bitCount(0) != 0) thrownew RuntimeException("o");
for (int i = 0; i < SIZE; i++) { int pow2 = 1 << i; if (bitCount(pow2) != 1) thrownew RuntimeException("p: " + i); if (bitCount(pow2 -1) != i) thrownew RuntimeException("q: " + i);
}
for (int i = 0; i < N; i++) { int x = rnd.nextInt(); if (bitCount(x) != bitCount(reverse(x))) thrownew RuntimeException("r: " + toHexString(x));
}
for (int i = 0; i < N; i++) { int x = rnd.nextInt(); int dist = rnd.nextInt(); if (bitCount(x) != bitCount(rotateRight(x, dist))) thrownew RuntimeException("s: " + toHexString(x) +
toHexString(dist)); if (bitCount(x) != bitCount(rotateLeft(x, dist))) thrownew RuntimeException("t: " + toHexString(x) +
toHexString(dist)); if (rotateRight(x, dist) != rotateLeft(x, -dist)) thrownew RuntimeException("u: " + toHexString(x) +
toHexString(dist)); if (rotateRight(x, -dist) != rotateLeft(x, dist)) thrownew RuntimeException("v: " + toHexString(x) +
toHexString(dist));
}
for (int i = 0; i < N; i++) { int x = rnd.nextInt(); if (bitCount(x) != bitCount(reverseBytes(x))) thrownew RuntimeException("z: " + toHexString(x));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.