Here you can find the source of setBit(byte[] bytes, int bitNr, int bit)
public static void setBit(byte[] bytes, int bitNr, int bit)
//package com.java2s; //License from project: Apache License 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 {/*from ww w. j a v a 2s. co m*/ bytes[byteNr] &= ~(1 << bitNrInByte); } } }