1 /* 2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4495754 27 * @summary Basic test for long bit twiddling 28 * @author Josh Bloch 29 */ 30 31 import java.util.Random; 32 import static java.lang.Long.*; 33 34 public class BitTwiddle { 35 private static final int N = 1000; // # of repetitions per test 36 37 public static void main(String args[]) { 38 Random rnd = new Random(); 39 40 if (highestOneBit(0) != 0) 41 throw new RuntimeException("a"); 42 if (highestOneBit(-1) != MIN_VALUE) 43 throw new RuntimeException("b"); 44 if (highestOneBit(1) != 1) 45 throw new RuntimeException("c"); 46 47 if (lowestOneBit(0) != 0) 48 throw new RuntimeException("d"); 49 if (lowestOneBit(-1) != 1) 50 throw new RuntimeException("e"); 51 if (lowestOneBit(MIN_VALUE) != MIN_VALUE) 52 throw new RuntimeException("f"); 53 54 for (int i = 0; i < N; i++) { 55 long x = rnd.nextLong(); 56 if (highestOneBit(x) != reverse(lowestOneBit(reverse(x)))) 57 throw new RuntimeException("g: " + toHexString(x)); 58 } 59 60 if (numberOfLeadingZeros(0) != SIZE) 61 throw new RuntimeException("h"); 62 if (numberOfLeadingZeros(-1) != 0) 63 throw new RuntimeException("i"); 64 if (numberOfLeadingZeros(1) != (SIZE - 1)) 65 throw new RuntimeException("j"); 66 67 if (numberOfTrailingZeros(0) != SIZE) 68 throw new RuntimeException("k"); 69 if (numberOfTrailingZeros(1) != 0) 70 throw new RuntimeException("l"); 71 if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1)) 72 throw new RuntimeException("m"); 73 74 for (int i = 0; i < N; i++) { 75 long x = rnd.nextLong(); 76 if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x))) 77 throw new RuntimeException("n: " + toHexString(x)); 78 } 79 80 if (bitCount(0) != 0) 81 throw new RuntimeException("o"); 82 83 for (int i = 0; i < SIZE; i++) { 84 long pow2 = 1L << i; 85 if (bitCount(pow2) != 1) 86 throw new RuntimeException("p: " + i); 87 if (bitCount(pow2 -1) != i) 88 throw new RuntimeException("q: " + i); 89 } 90 91 for (int i = 0; i < N; i++) { 92 long x = rnd.nextLong(); 93 if (bitCount(x) != bitCount(reverse(x))) 94 throw new RuntimeException("r: " + toHexString(x)); 95 } 96 97 for (int i = 0; i < N; i++) { 98 long x = rnd.nextLong(); 99 int dist = rnd.nextInt(); 100 if (bitCount(x) != bitCount(rotateRight(x, dist))) 101 throw new RuntimeException("s: " + toHexString(x) + 102 toHexString(dist)); 103 if (bitCount(x) != bitCount(rotateLeft(x, dist))) 104 throw new RuntimeException("t: " + toHexString(x) + 105 toHexString(dist)); 106 if (rotateRight(x, dist) != rotateLeft(x, -dist)) 107 throw new RuntimeException("u: " + toHexString(x) + 108 toHexString(dist)); 109 if (rotateRight(x, -dist) != rotateLeft(x, dist)) 110 throw new RuntimeException("v: " + toHexString(x) + 111 toHexString(dist)); 112 } 113 114 if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1 115 || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1) 116 throw new RuntimeException("w"); 117 118 for (int i = 0; i < N; i++) { 119 long x = rnd.nextLong(); 120 int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1)); 121 if (signum(x) != sign) 122 throw new RuntimeException("x: " + toHexString(x)); 123 } 124 125 if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL) 126 throw new RuntimeException("y"); 127 128 for (int i = 0; i < N; i++) { 129 long x = rnd.nextLong(); 130 if (bitCount(x) != bitCount(reverseBytes(x))) 131 throw new RuntimeException("z: " + toHexString(x)); 132 } 133 } 134 }