Java Byte to Boolean byteToBoolArr(final byte value)

Here you can find the source of byteToBoolArr(final byte value)

Description

Gets the bit states from a byte as boolean[].

License

Open Source License

Parameter

Parameter Description
value The <code>byte</code> value

Return

The bit states as boolean[]

Declaration

public static boolean[] byteToBoolArr(final byte value) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .  j a v a2  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 {
    /**
     * Constants for the length in byte of Java primitve datatypes.
     * @since 1.0
     */
    public final static byte BYTE_BIT_LENGTH = 8;
    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 states from a <code>byte</code> as <code>boolean[]</code>.
     * @param value The <code>byte</code> value
     * @return The bit states as <code>boolean[]</code>
     * @since 1.0
     */
    public static boolean[] byteToBoolArr(final byte value) {
        final boolean[] result = new boolean[BYTE_BIT_LENGTH];

        for (int i = 0; i < result.length; i++) {
            result[i] = bitToBoolean(value, i);
        }

        return result;
    }

    /**
     * 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. byteToBoolean(byte b)
  2. ByteToBoolean(byte value)
  3. byteToBoolean(final byte b)
  4. byteToboolean(final byte value)