1 /*
   2  * Copyright (c) 2003, 2017, 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 /test/lib
  27  * @run main BitTwiddle
  28  * @bug     4495754 8078672
  29  * @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed)
  30  * @author  Josh Bloch
  31  * @key randomness
  32  */
  33 
  34 import java.util.Random;
  35 import jdk.test.lib.RandomFactory;
  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 
  61             String expected = new StringBuilder()
  62                 .append(leftpad(toBinaryString(x), 32))
  63                 .reverse().toString();
  64 
  65             String actual = leftpad(toBinaryString(reverse(x)), 32);
  66 
  67             if (!expected.equals(actual)) {
  68                 throw new RuntimeException("reverse: \n" +
  69                         expected + " \n" + actual);
  70             }
  71         }
  72 
  73         for (int i = 0; i < N; i++) {
  74             int x = rnd.nextInt();
  75             if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
  76                 throw new RuntimeException("g: " + toHexString(x));
  77         }
  78 
  79         if (numberOfLeadingZeros(0) != SIZE)
  80             throw new RuntimeException("h");
  81         if (numberOfLeadingZeros(-1) != 0)
  82             throw new RuntimeException("i");
  83         if (numberOfLeadingZeros(1) != (SIZE - 1))
  84             throw new RuntimeException("j");
  85 
  86         if (numberOfTrailingZeros(0) != SIZE)
  87             throw new RuntimeException("k");
  88         if (numberOfTrailingZeros(1) != 0)
  89             throw new RuntimeException("l");
  90         if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
  91             throw new RuntimeException("m");
  92 
  93         for (int i = 0; i < N; i++) {
  94             int x = rnd.nextInt();
  95             if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
  96                 throw new RuntimeException("n: " + toHexString(x));
  97         }
  98 
  99         if (bitCount(0) != 0)
 100                 throw new RuntimeException("o");
 101 
 102         for (int i = 0; i < SIZE; i++) {
 103             int pow2 = 1 << i;
 104             if (bitCount(pow2) != 1)
 105                 throw new RuntimeException("p: " + i);
 106             if (bitCount(pow2 -1) != i)
 107                 throw new RuntimeException("q: " + i);
 108         }
 109 
 110         for (int i = 0; i < N; i++) {
 111             int x = rnd.nextInt();
 112             if (bitCount(x) != bitCount(reverse(x)))
 113                 throw new RuntimeException("r: " + toHexString(x));
 114         }
 115 
 116         for (int i = 0; i < N; i++) {
 117             int x = rnd.nextInt();
 118             int dist = rnd.nextInt();
 119             if (bitCount(x) != bitCount(rotateRight(x, dist)))
 120                 throw new RuntimeException("s: " + toHexString(x) +
 121                                            toHexString(dist));
 122             if (bitCount(x) != bitCount(rotateLeft(x, dist)))
 123                 throw new RuntimeException("t: " + toHexString(x) +
 124                                            toHexString(dist));
 125             if (rotateRight(x, dist) != rotateLeft(x, -dist))
 126                 throw new RuntimeException("u: " + toHexString(x) +
 127                                            toHexString(dist));
 128             if (rotateRight(x, -dist) != rotateLeft(x, dist))
 129                 throw new RuntimeException("v: " + toHexString(x) +
 130                                            toHexString(dist));
 131         }
 132 
 133         if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
 134             || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
 135             throw new RuntimeException("w");
 136 
 137         for (int i = 0; i < N; i++) {
 138             int x = rnd.nextInt();
 139             int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
 140             if (signum(x) != sign)
 141                 throw new RuntimeException("x: " + toHexString(x));
 142         }
 143 
 144         if(reverseBytes(0xaabbccdd) != 0xddccbbaa)
 145             throw new RuntimeException("y");
 146 
 147         for (int i = 0; i < N; i++) {
 148             int x = rnd.nextInt();
 149             if (bitCount(x) != bitCount(reverseBytes(x)))
 150                 throw new RuntimeException("z: " + toHexString(x));
 151         }
 152     }
 153 
 154     private static String leftpad(String s, int width) {
 155         String r = s;
 156         for (int c = 0; c < width - s.length(); c++) {
 157             r = "0" + r;
 158         }
 159         return r;
 160     }
 161 }