Java BigInteger Calculate listToBigInteger(List list)

Here you can find the source of listToBigInteger(List list)

Description

Transforms a List of Integer s representing non-zero bits into a BigInteger of the represented value.

License

Apache License

Parameter

Parameter Description
list a parameter

Declaration

public static BigInteger listToBigInteger(List<Integer> list) 

Method Source Code


//package com.java2s;
/*/*from www.  ja  va 2 s .c  o m*/
 * Copyright 2015-2016 USEF Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigInteger;

import java.util.List;

public class Main {
    /**
     * Transforms a List of {@link Integer}s representing non-zero bits into a {@Link BigInteger} of the represented value.
     * <p/>
     * Example: a list with {@Link Integer} values 2, 3 and 5 is transformed into a {@Link BigInteger} with value 22.
     *
     * @param list
     * @return
     */
    public static BigInteger listToBigInteger(List<Integer> list) {
        if (list == null || list.isEmpty()) {
            return BigInteger.ZERO;
        }
        list.sort((i1, i2) -> i1 > i2 ? 1 : -1);
        int max = list.get(list.size() - 1);
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < max; ++i) {
            buffer.append('0');
        }
        for (Integer integer : list) {
            buffer.setCharAt(max - integer, '1');
        }
        return new BigInteger(buffer.toString(), 2);
    }
}

Related

  1. integerToString(BigInteger value)
  2. intValue(BigInteger bi)
  3. intValueExact(BigInteger bigint)
  4. jsonBigInteger(JsonValue value)
  5. length(BigInteger bi)
  6. log2(BigInteger x)
  7. maskBits(BigInteger value, int bits)
  8. min(BigInteger a, BigInteger b)
  9. modPow(BigInteger base, BigInteger e, BigInteger m)