Example usage for java.math BigInteger testBit

List of usage examples for java.math BigInteger testBit

Introduction

In this page you can find the example usage for java.math BigInteger testBit.

Prototype

public boolean testBit(int n) 

Source Link

Document

Returns true if and only if the designated bit is set.

Usage

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero./*  w  w w.  ja  v a 2  s. c o  m*/
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:org.opendaylight.iotdm.onem2m.core.resource.ResourceAccessControlPolicy.java

public static boolean isAllowedThisOperation(String OperationNumber, BigInteger number) {
    switch (OperationNumber) {
    case Onem2m.Operation.CREATE:
        if (number.testBit(0))
            return true;
        break;/*from  w w  w .  j av a 2s  .  com*/
    case Onem2m.Operation.RETRIEVE:
        if (number.testBit(1))
            return true;
        break;
    case Onem2m.Operation.UPDATE:
        if (number.testBit(2))
            return true;
        break;
    case Onem2m.Operation.DELETE:
        if (number.testBit(3))
            return true;
        break;
    case Onem2m.Operation.DISCOVER:
        if (number.testBit(4))
            return true;
        break;
    case Onem2m.Operation.NOTIFY:
        if (number.testBit(5))
            return true;
        break;
    default:
        return false;
    }
    return false;

}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.// w ww.  j  a  v  a2s  .  c o  m
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }//w w w .j a  v a2s.  c o  m

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }/* www  .  j a v a  2  s . co  m*/

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }
    if (k.signum() == 0) {
        return EMPTY_INTS;
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:org.openhab.binding.ulux.internal.ump.messages.EventMessage.java

public EventMessage(final short actorId, final ByteBuffer data) {
    super((byte) 0x06, UluxMessageId.Event, actorId, data);

    // key state//w w  w .ja v a 2  s.c  om
    final BigInteger keyState = BigInteger.valueOf(data.get());
    this.keyState.put(Key.KEY_1, keyState.testBit(0));
    this.keyState.put(Key.KEY_2, keyState.testBit(1));
    this.keyState.put(Key.KEY_3, keyState.testBit(2));
    this.keyState.put(Key.KEY_4, keyState.testBit(3));

    // reserved
    data.get();
}

From source file:org.openhab.binding.ulux.internal.ump.messages.StateMessage.java

public StateMessage(final short actorId, final ByteBuffer data) {
    super((byte) 0x04, UluxMessageId.State, actorId, data);

    final BigInteger stateFlags = BigInteger.valueOf(data.getInt());

    this.i2cHumidityValid = stateFlags.testBit(25);
    this.i2cTemperatureValid = stateFlags.testBit(24);
    this.motionSensor = stateFlags.testBit(11);
    this.internalError = stateFlags.testBit(7);
    this.initRequest = stateFlags.testBit(6);
    this.timeRequest = stateFlags.testBit(5);
    this.introActive = stateFlags.testBit(4);
    this.audioActive = stateFlags.testBit(3);
    this.displayActive = stateFlags.testBit(2);
    this.proximitySensor = stateFlags.testBit(1);
    this.lightSensor = stateFlags.testBit(0);
}

From source file:org.openhab.binding.ulux.internal.ump.messages.ControlMessage.java

private void parse(final ByteBuffer data) {
    final BigInteger controlFlags = BigInteger.valueOf(data.getInt());

    this.i2cPlugAndPlay = controlFlags.testBit(31);
    this.i2cHumidityChangeRequest = controlFlags.testBit(25);
    this.i2cTemperatureChangeRequest = controlFlags.testBit(24);
    this.motionSensorChangeRequest = controlFlags.testBit(11);
    this.keepAlive = controlFlags.testBit(10);
    this.changeFilter = controlFlags.testBit(9);
    this.frameAcknowledgement = controlFlags.testBit(8);
    this.volumeChangeRequest = controlFlags.testBit(5);
    this.pageChangeRequest = controlFlags.testBit(4);
    this.audioActiveChangeRequest = controlFlags.testBit(3);
    this.displayActiveChangeRequest = controlFlags.testBit(2);
    this.proximitySensorChangeRequest = controlFlags.testBit(1);
    this.lightSensorChangeRequest = controlFlags.testBit(0);

    boolean lockMode0 = controlFlags.testBit(12);
    boolean lockMode1 = controlFlags.testBit(13);
    if (lockMode0) {
        if (lockMode1) {
            this.lockMode = LockMode.ALL_LOGO;
        } else {// w  w  w.j a va2  s .c o m
            this.lockMode = LockMode.ALL; // TODO verify
        }
    } else {
        if (lockMode1) {
            this.lockMode = LockMode.NAVIGATION; // TODO verify
        } else {
            this.lockMode = LockMode.NONE;
        }
    }

    boolean backgroundLight0 = controlFlags.testBit(14);
    boolean backgroundLight1 = controlFlags.testBit(15);
    if (backgroundLight0) {
        if (backgroundLight1) {
            this.backgroundLight = BackgroundLight.ON;
        } else {
            this.backgroundLight = BackgroundLight.OFF; // TODO verify
        }
    } else {
        if (backgroundLight1) {
            this.backgroundLight = BackgroundLight.AUTO_NIGHT; // TODO
            // verify
        } else {
            this.backgroundLight = BackgroundLight.AUTO_DAY;
        }
    }
}

From source file:com.google.uzaygezen.core.hbase.HBaseQueryTest.java

public List<int[]> queryAndFilter(MockHTable table, MultiDimensionalSpec spec, SpaceFillingCurve sfc,
        int[][] ranges, int maxRanges, Map<Pow2LengthBitSetRange, NodeValue<BigIntegerContent>> rolledupMap)
        throws IOException {
    List<BigIntegerRange> region = rangesToQueryRegion(ranges);
    List<FilteredIndexRange<Object, BigIntegerRange>> indexRanges = query(table, region, sfc, maxRanges,
            rolledupMap);/* ww  w  . j  a  v  a  2  s .  c o m*/
    Assert.assertTrue(indexRanges.size() <= maxRanges);
    logger.log(Level.INFO, "indexRanges={0}", indexRanges);
    // The ranges are in strictly increasing hilbert index order.
    for (int i = 0; i < indexRanges.size() - 1; ++i) {
        FilteredIndexRange<Object, BigIntegerRange> a = indexRanges.get(i);
        FilteredIndexRange<Object, BigIntegerRange> b = indexRanges.get(i + 1);
        Assert.assertTrue(a.getIndexRange().getEnd().compareTo(b.getIndexRange().getStart()) < 0);
    }
    BitVector start = BitVectorFactories.OPTIMAL.apply(spec.sumBitsPerDimension());
    BitVector end = BitVectorFactories.OPTIMAL.apply(spec.sumBitsPerDimension());
    Scan[] scans = new Scan[indexRanges.size()];
    for (int i = 0; i < indexRanges.size(); ++i) {
        FilteredIndexRange<Object, BigIntegerRange> indexRange = indexRanges.get(i);
        BigInteger startBigInteger = indexRange.getIndexRange().getStart();
        start.copyFrom(startBigInteger);
        BigInteger endBigInteger = indexRange.getIndexRange().getEnd();
        final Scan scan;
        if (endBigInteger.testBit(spec.sumBitsPerDimension())) {
            scan = new Scan(start.toBigEndianByteArray());
        } else {
            end.copyFrom(endBigInteger);
            scan = new Scan(start.toBigEndianByteArray(), end.toBigEndianByteArray());
        }
        scans[i] = scan;
    }
    BitVector[] point = new BitVector[spec.getBitsPerDimension().size()];
    BitVector index = BitVectorFactories.OPTIMAL.apply(spec.sumBitsPerDimension());
    for (int j = 0; j < spec.getBitsPerDimension().size(); ++j) {
        point[j] = BitVectorFactories.OPTIMAL.apply(spec.getBitsPerDimension().get(j));
    }
    List<int[]> actual = new ArrayList<>();
    for (int i = 0; i < indexRanges.size(); ++i) {
        ResultScanner scanner = table.getScanner(scans[i]);
        FilteredIndexRange<Object, BigIntegerRange> indexRange = indexRanges.get(i);
        logger.log(Level.FINE, "indexRange={0}", indexRange);
        for (Result result : scanner) {
            byte[] row = result.getRow();
            index.copyFromBigEndian(row);
            sfc.indexInverse(index, point);
            boolean isContained = RangeUtil.containsBigInteger(region,
                    Arrays.asList(bitVectorPointToBigIntegerPoint(point)));
            if (!indexRange.isPotentialOverSelectivity()) {
                Assert.assertTrue(isContained);
            }
            if (isContained) {
                int[] e = new int[point.length];
                for (int j = 0; j < e.length; ++j) {
                    e[j] = (int) point[j].toExactLong();
                }
                actual.add(e);
            }
        }
    }
    return actual;
}

From source file:com.acmemotors.obd2.OBD2Controller.java

public List<String> decodePidsSupported(String response, int offset) {
    List<String> pids = new ArrayList<>();

    String[] respsonseLines = response.split("\n");

    StringBuilder pidString = new StringBuilder();

    String[] elements = respsonseLines[1].split(" ");

    for (int j = 7; j < elements.length; j++) {
        pidString.append(elements[j]);/*from ww  w. j av a  2  s . co  m*/
    }

    BigInteger bigInt = new BigInteger(pidString.toString(), 16);

    int j = 0;
    for (int i = bigInt.bitLength() - 1; i >= 0; i--) {
        int pidId = j + offset + 1;

        if (bigInt.testBit(i)) {
            pids.add(Integer.toHexString(pidId));
        }

        j++;
    }

    return pids;
}