Java BigInteger Calculate shiftLeft(final BigInteger register, final BigInteger input, final int n)

Here you can find the source of shiftLeft(final BigInteger register, final BigInteger input, final int n)

Description

Shifts register left by n bits and sets these n bits to the n rightmost bits of input.

License

Open Source License

Parameter

Parameter Description
register the BigInteger to be shifted to the left.
input the BigInteger that will be appended to <code>register</code>.
n the number of bits to shift <code>register</code> left and the number of rightmost bits of <code>input</code> that will be appended.

Return

a BigInteger with the n rightmost bits of input appended to register.

Declaration

public static BigInteger shiftLeft(final BigInteger register, final BigInteger input, final int n) 

Method Source Code


//package com.java2s;
/*/*from w  w w  . ja v a  2  s.c  o  m*/
 * Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.math.BigInteger;

public class Main {
    /**
     * Shifts <code>register</code> left by n bits and sets these n bits to the
     * n rightmost bits of <code>input</code>.
     * 
     * @param register
     *            the BigInteger to be shifted to the left.
     * @param input
     *            the BigInteger that will be appended to <code>register</code>.
     * @param n
     *            the number of bits to shift <code>register</code> left and the
     *            number of rightmost bits of <code>input</code> that will be
     *            appended.
     * @return a BigInteger with the n rightmost bits of <code>input</code>
     *         appended to <code>register</code>.
     */
    public static BigInteger shiftLeft(final BigInteger register, final BigInteger input, final int n) {
        // make sure only the last n bits of input are set
        final BigInteger cutSuffix = getNRightmostBits(input, n);
        return register.shiftLeft(n).or(cutSuffix);
    }

    /**
     * Returns a BigInteger with the <code>n</code> rightmost bits of
     * <code>in</code>.
     * 
     * @param in
     *            the BigInteger of which the n rightmost bits are of interest.
     * @param n
     *            the number of bits from the right of in that are of interest
     *            (n &gt;= 0, and probably n &lt;= in.bitLength()).
     * 
     * @return a BigInteger with the BTREE rightmost bits of <code>in</code>.
     */
    public static BigInteger getNRightmostBits(final BigInteger in, final int n) {
        // make sure only the last n bits are set
        final BigInteger mask = BigInteger.valueOf(2).pow(n).subtract(BigInteger.ONE);
        // BigInteger mask = BigInteger.valueOf(Math.round(Math.pow(2, n)) - 1);
        return in.and(mask);

        /*
         * TODO: This code does not work (because of sign extension??)
         * BigInteger negMask = BigInteger.ONE.shiftLeft(n); return
         * in.andNot(negMask);
         */
    }
}

Related

  1. saveToFile(String fileName, BigInteger mod, BigInteger exp)
  2. serializeModexpNoBase(BigInteger[] modexp, boolean withResult)
  3. serializeModexpResponse(BigInteger response)
  4. serializeQuery(BigInteger modulus, BigInteger base, BigInteger exponent, boolean brief, String... modexps)
  5. shift(BigInteger integer, int distance)
  6. split(BigInteger maxValue, int numberOfSplits)
  7. splitBigInt(BigInteger value, int n)
  8. splitFloat(BigInteger rawFloat, int signWidth, int exponentWidth, int mantissaWidth)
  9. sqrt(BigInteger n)