Android Bit Get getBit(byte[] bytes, int bitNr)

Here you can find the source of getBit(byte[] bytes, int bitNr)

Description

Get the given bit in the encoded bytes.

License

Apache License

Parameter

Parameter Description
bytes a parameter
bitNr a parameter

Declaration

public static int getBit(byte[] bytes, int bitNr) 

Method Source Code

//package com.java2s;
/**/*  w  w  w .  j  a v  a 2  s  . co m*/
 * Source obtained from crypto-gwt. Apache 2 License.
 * https://code.google.com/p/crypto-gwt/source/browse/crypto-gwt/src/main/java/com/googlecode/
 * cryptogwt/util/ByteArrayUtils.java
 */

public class Main {
    /**
     * Get the given bit in the encoded bytes.  Note: bit 0 is assumed to be the LSB.
     *
     * @param bytes
     * @param bitNr
     * @return
     */
    public static int getBit(byte[] bytes, int bitNr) {
        int byteNr = bytes.length - (bitNr / 8) - 1;
        int bitNrInByte = bitNr % 8;
        return bytes[byteNr] >>> bitNrInByte & 1;

    }
}

Related

  1. getMostSignificantBit(byte b)