Here you can find the source of listToBigInteger(List
Parameter | Description |
---|---|
list | a parameter |
public static BigInteger listToBigInteger(List<Integer> list)
//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); } }