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; /**/*from www . j a va 2s.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 { 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); } } }