Get the given bit in the encoded bytes. - Android java.lang

Android examples for java.lang:String Unicode

Description

Get the given bit in the encoded bytes.

Demo Code

/**//w w w .  j a v a  2 s . c o  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
 */
//package com.book2s;

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 Tutorials