set Bit - Android File Input Output

Android examples for File Input Output:Byte Array

Description

set Bit

Demo Code

/**/*  ww  w  . ja v  a  2s.  c  om*/
 * 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 {
    public static void setBit(byte[] bytes, int bitNr, int bit) {
        int byteNr = bytes.length - (bitNr / 8) - 1;
        int bitNrInByte = bitNr % 8;
        if (bit != 0) {
            bytes[byteNr] |= 1 << bitNrInByte;
        } else {
            bytes[byteNr] &= ~(1 << bitNrInByte);
        }
    }
}

Related Tutorials