Path: blob/master/test/jdk/java/lang/Long/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 long 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.Long.*;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++) {59long x = rnd.nextLong();6061String expected = new StringBuilder()62.append(leftpad(toBinaryString(x), 64))63.reverse().toString();6465String actual = leftpad(toBinaryString(reverse(x)), 64);6667if (!expected.equals(actual)) {68throw new RuntimeException("reverse: \n" +69expected + " \n" + actual);70}71}7273for (int i = 0; i < N; i++) {74long x = rnd.nextLong();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(Long.MAX_VALUE) != 1)86throw new RuntimeException("lzmax");87if (numberOfLeadingZeros(Integer.MAX_VALUE + 1L) != (SIZE - 32))88throw new RuntimeException("lz32");8990if (numberOfTrailingZeros(0) != SIZE)91throw new RuntimeException("k");92if (numberOfTrailingZeros(1) != 0)93throw new RuntimeException("l");94if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))95throw new RuntimeException("m");9697for (int i = 0; i < N; i++) {98long x = rnd.nextLong();99if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))100throw new RuntimeException("n: " + toHexString(x));101}102for (long i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {103if (numberOfLeadingZeros(i) != (int)r ||104numberOfTrailingZeros(i) != (SIZE - (int)r - 1) ||105numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {106throw new RuntimeException("lzx: " + toHexString(i));107}108}109110if (bitCount(0) != 0)111throw new RuntimeException("o");112113for (int i = 0; i < SIZE; i++) {114long pow2 = 1L << i;115if (bitCount(pow2) != 1)116throw new RuntimeException("p: " + i);117if (bitCount(pow2 -1) != i)118throw new RuntimeException("q: " + i);119}120121for (int i = 0; i < N; i++) {122long x = rnd.nextLong();123if (bitCount(x) != bitCount(reverse(x)))124throw new RuntimeException("r: " + toHexString(x));125}126127for (int i = 0; i < N; i++) {128long x = rnd.nextLong();129int dist = rnd.nextInt();130if (bitCount(x) != bitCount(rotateRight(x, dist)))131throw new RuntimeException("s: " + toHexString(x) +132toHexString(dist));133if (bitCount(x) != bitCount(rotateLeft(x, dist)))134throw new RuntimeException("t: " + toHexString(x) +135toHexString(dist));136if (rotateRight(x, dist) != rotateLeft(x, -dist))137throw new RuntimeException("u: " + toHexString(x) +138toHexString(dist));139if (rotateRight(x, -dist) != rotateLeft(x, dist))140throw new RuntimeException("v: " + toHexString(x) +141toHexString(dist));142}143144if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1145|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)146throw new RuntimeException("w");147148for (int i = 0; i < N; i++) {149long x = rnd.nextLong();150int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));151if (signum(x) != sign)152throw new RuntimeException("x: " + toHexString(x));153}154155if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)156throw new RuntimeException("y");157158for (int i = 0; i < N; i++) {159long x = rnd.nextLong();160if (bitCount(x) != bitCount(reverseBytes(x)))161throw new RuntimeException("z: " + toHexString(x));162}163}164165private static final String ZEROS = "0".repeat(64);166private static String leftpad(String s, int width) {167return ZEROS.substring(0, width - s.length()) + s;168}169}170171172