Here you can find the source of setBit(byte[] ba, int bit_offset, boolean on)
public static void setBit(byte[] ba, int bit_offset, boolean on)
//package com.java2s; //License from project: Apache License public class Main { public static void setBit(byte[] ba, int bit_offset, boolean on) { int byte_idx = bit_offset / 8; int bit_remainder = bit_offset % 8; byte bit_mask = (byte) ((0x1 << (7 - bit_remainder)) & 0xFF); // System.out.println("byte_idx="+byte_idx+" // bit_mask="+Long.toHexString((long)bit_mask)); if (on) { ba[byte_idx] |= bit_mask; } else {/* w ww . ja v a 2 s . co m*/ ba[byte_idx] &= ~bit_mask; } } }