Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Long/BitTwiddle.java
38813 views
/*1* Copyright (c) 2003, 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* @bug 449575426* @summary Basic test for long bit twiddling27* @author Josh Bloch28* @key randomness29*/3031import java.util.Random;32import static java.lang.Long.*;3334public class BitTwiddle {35private static final int N = 1000; // # of repetitions per test3637public static void main(String args[]) {38Random rnd = new Random();3940if (highestOneBit(0) != 0)41throw new RuntimeException("a");42if (highestOneBit(-1) != MIN_VALUE)43throw new RuntimeException("b");44if (highestOneBit(1) != 1)45throw new RuntimeException("c");4647if (lowestOneBit(0) != 0)48throw new RuntimeException("d");49if (lowestOneBit(-1) != 1)50throw new RuntimeException("e");51if (lowestOneBit(MIN_VALUE) != MIN_VALUE)52throw new RuntimeException("f");5354for (int i = 0; i < N; i++) {55long x = rnd.nextLong();56if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))57throw new RuntimeException("g: " + toHexString(x));58}5960if (numberOfLeadingZeros(0) != SIZE)61throw new RuntimeException("h");62if (numberOfLeadingZeros(-1) != 0)63throw new RuntimeException("i");64if (numberOfLeadingZeros(1) != (SIZE - 1))65throw new RuntimeException("j");6667if (numberOfTrailingZeros(0) != SIZE)68throw new RuntimeException("k");69if (numberOfTrailingZeros(1) != 0)70throw new RuntimeException("l");71if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))72throw new RuntimeException("m");7374for (int i = 0; i < N; i++) {75long x = rnd.nextLong();76if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))77throw new RuntimeException("n: " + toHexString(x));78}7980if (bitCount(0) != 0)81throw new RuntimeException("o");8283for (int i = 0; i < SIZE; i++) {84long pow2 = 1L << i;85if (bitCount(pow2) != 1)86throw new RuntimeException("p: " + i);87if (bitCount(pow2 -1) != i)88throw new RuntimeException("q: " + i);89}9091for (int i = 0; i < N; i++) {92long x = rnd.nextLong();93if (bitCount(x) != bitCount(reverse(x)))94throw new RuntimeException("r: " + toHexString(x));95}9697for (int i = 0; i < N; i++) {98long x = rnd.nextLong();99int dist = rnd.nextInt();100if (bitCount(x) != bitCount(rotateRight(x, dist)))101throw new RuntimeException("s: " + toHexString(x) +102toHexString(dist));103if (bitCount(x) != bitCount(rotateLeft(x, dist)))104throw new RuntimeException("t: " + toHexString(x) +105toHexString(dist));106if (rotateRight(x, dist) != rotateLeft(x, -dist))107throw new RuntimeException("u: " + toHexString(x) +108toHexString(dist));109if (rotateRight(x, -dist) != rotateLeft(x, dist))110throw new RuntimeException("v: " + toHexString(x) +111toHexString(dist));112}113114if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1115|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)116throw new RuntimeException("w");117118for (int i = 0; i < N; i++) {119long x = rnd.nextLong();120int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));121if (signum(x) != sign)122throw new RuntimeException("x: " + toHexString(x));123}124125if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)126throw new RuntimeException("y");127128for (int i = 0; i < N; i++) {129long x = rnd.nextLong();130if (bitCount(x) != bitCount(reverseBytes(x)))131throw new RuntimeException("z: " + toHexString(x));132}133}134}135136137