Java Bits to Int bitsToInt(boolean[] pBits)

Here you can find the source of bitsToInt(boolean[] pBits)

Description

Gets the integer represented by a bit string (as an array).

License

Open Source License

Parameter

Parameter Description
pBits boolean array representing the bits (should, of course, never be longer than the length of an integer)

Return

int represented by this bit string (most important bit is leftmost)

Declaration

public static int bitsToInt(boolean[] pBits) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2002 The European Commission DREAM Project IST-1999-12679
 *
 * This file is part of JEO./*from  w w  w .j  av  a 2s. c  o m*/
 * JEO is free software; you can redistribute it and/or modify it under the terms of GNU 
 * General Public License as published by the Free Sortware Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JEO 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 mor details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 59 TEmple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */

public class Main {
    /**
     * Gets the integer represented by a bit string (as an array).
     * @param pBits boolean array representing the bits (should, of course,
     * never be longer than the length of an integer)
     * @return int represented by this bit string (most important bit is leftmost)
     */
    public static int bitsToInt(boolean[] pBits) {
        int vInt = 0;
        int vIntBit = 0;
        for (int i = pBits.length - 1; i >= 0; i--) {
            if (pBits[i]) {
                vInt |= (1 << vIntBit);
            }
            vIntBit++;
        }
        return vInt;
    }
}

Related

  1. bitsToInt(String bits, int min, int max)
  2. bitsToIntArray(String bits, int min, int max, int numBits, int size)