Java Bits Convert to bitToBoolean(final byte value, final int bitNbr)

Here you can find the source of bitToBoolean(final byte value, final int bitNbr)

Description

Gets the bit state of a specific bit from a byte as boolean.

License

Open Source License

Parameter

Parameter Description
value The <code>byte</code> value
bitNbr The specific bit in the <code>byte</code>, from 0 to 7

Return

The bit state as boolean.

Declaration

public static boolean bitToBoolean(final byte value, final int bitNbr) 

Method Source Code

//package com.java2s;
/**/*from  w ww . j  ava2  s .c o  m*/
 * jModuleConnect is an framework for communication and file management on modem 
 * modules.
 * 
 * This project was inspired by the project TC65SH 
 * by Christoph Vilsmeier: <http://www.vilsmeier-consulting.de/tc65sh.html>
 * 
 * Copyright (C) 2015 sitec systems GmbH <http://www.sitec-systems.de>
 * 
 * This file is part of jModuleConnect.
 * 
 * jModuleConnect is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * jModuleConnect 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 Lesser General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with jModuleConnect. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private final static byte[] BIT = new byte[] { (byte) 0x01, (byte) 0x02, (byte) 0x04, (byte) 0x08, (byte) 0x010,
            (byte) 0x20, (byte) 0x40, (byte) 0x80 };

    /**
     * Gets the bit state of a specific bit from a <code>byte</code> as <code>
     * boolean</code>.
     * @param value The <code>byte</code> value
     * @param bitNbr The specific bit in the <code>byte</code>, from 0 to 7
     * @return The bit state as <code>boolean</code>.
     * @since 1.0
     */
    public static boolean bitToBoolean(final byte value, final int bitNbr) {
        if (bitNbr < 0 || bitNbr > 7) {
            throw new IllegalArgumentException("Parameter bitNbr is out of range (0-7)");
        }

        return (value & BIT[bitNbr]) != 0;
    }
}

Related

  1. bitString2byte(String str)
  2. bitStringToInt(String bits, char one)
  3. bitsUsed(long value)
  4. bitTest(String value, int pos)
  5. bitToBoolean(byte b)
  6. bitToLong(byte[] bytes, int offset, int length)
  7. bitToLong(byte[] bytes, int offset, int length)
  8. bitValueHelper(final int index, final int[] data)
  9. bitWiseAnd(final byte[] array1, final byte[] array2)