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