Path: blob/master/test/jdk/java/lang/Integer/BitTwiddle.java
41149 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main BitTwiddle28* @bug 4495754 807867229* @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed)30* @author Josh Bloch31* @key randomness32*/3334import java.util.Random;35import jdk.test.lib.RandomFactory;36import static java.lang.Integer.*;3738public class BitTwiddle {39private static final int N = 1000; // # of repetitions per test4041public static void main(String args[]) {42Random rnd = RandomFactory.getRandom();4344if (highestOneBit(0) != 0)45throw new RuntimeException("a");46if (highestOneBit(-1) != MIN_VALUE)47throw new RuntimeException("b");48if (highestOneBit(1) != 1)49throw new RuntimeException("c");5051if (lowestOneBit(0) != 0)52throw new RuntimeException("d");53if (lowestOneBit(-1) != 1)54throw new RuntimeException("e");55if (lowestOneBit(MIN_VALUE) != MIN_VALUE)56throw new RuntimeException("f");5758for (int i = 0; i < N; i++) {59int x = rnd.nextInt();6061String expected = new StringBuilder()62.append(leftpad(toBinaryString(x), 32))63.reverse().toString();6465String actual = leftpad(toBinaryString(reverse(x)), 32);6667if (!expected.equals(actual)) {68throw new RuntimeException("reverse: \n" +69expected + " \n" + actual);70}71}7273for (int i = 0; i < N; i++) {74int x = rnd.nextInt();75if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))76throw new RuntimeException("g: " + toHexString(x));77}7879if (numberOfLeadingZeros(0) != SIZE)80throw new RuntimeException("h");81if (numberOfLeadingZeros(-1) != 0)82throw new RuntimeException("i");83if (numberOfLeadingZeros(1) != (SIZE - 1))84throw new RuntimeException("j");85if (numberOfLeadingZeros(Integer.MAX_VALUE) != 1)86throw new RuntimeException("lzmax");8788if (numberOfTrailingZeros(0) != SIZE)89throw new RuntimeException("k");90if (numberOfTrailingZeros(1) != 0)91throw new RuntimeException("l");92if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))93throw new RuntimeException("m");9495for (int i = 0; i < N; i++) {96int x = rnd.nextInt();97if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))98throw new RuntimeException("n: " + toHexString(x));99}100for (int i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {101if (numberOfLeadingZeros(i) != r ||102numberOfTrailingZeros(i) != (SIZE - r - 1) ||103numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {104throw new RuntimeException("lzx: " + toHexString(i));105}106}107108if (bitCount(0) != 0)109throw new RuntimeException("o");110111for (int i = 0; i < SIZE; i++) {112int pow2 = 1 << i;113if (bitCount(pow2) != 1)114throw new RuntimeException("p: " + i);115if (bitCount(pow2 -1) != i)116throw new RuntimeException("q: " + i);117}118119for (int i = 0; i < N; i++) {120int x = rnd.nextInt();121if (bitCount(x) != bitCount(reverse(x)))122throw new RuntimeException("r: " + toHexString(x));123}124125for (int i = 0; i < N; i++) {126int x = rnd.nextInt();127int dist = rnd.nextInt();128if (bitCount(x) != bitCount(rotateRight(x, dist)))129throw new RuntimeException("s: " + toHexString(x) +130toHexString(dist));131if (bitCount(x) != bitCount(rotateLeft(x, dist)))132throw new RuntimeException("t: " + toHexString(x) +133toHexString(dist));134if (rotateRight(x, dist) != rotateLeft(x, -dist))135throw new RuntimeException("u: " + toHexString(x) +136toHexString(dist));137if (rotateRight(x, -dist) != rotateLeft(x, dist))138throw new RuntimeException("v: " + toHexString(x) +139toHexString(dist));140}141142if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1143|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)144throw new RuntimeException("w");145146for (int i = 0; i < N; i++) {147int x = rnd.nextInt();148int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));149if (signum(x) != sign)150throw new RuntimeException("x: " + toHexString(x));151}152153if(reverseBytes(0xaabbccdd) != 0xddccbbaa)154throw new RuntimeException("y");155156for (int i = 0; i < N; i++) {157int x = rnd.nextInt();158if (bitCount(x) != bitCount(reverseBytes(x)))159throw new RuntimeException("z: " + toHexString(x));160}161}162163private static final String ZEROS = "0".repeat(32);164private static String leftpad(String s, int width) {165return ZEROS.substring(0, width - s.length()) + s;166}167}168169170