Here you can find the source of setBit(byte[] data, int pos, int val)
public static byte[] setBit(byte[] data, int pos, int val)
//package com.java2s; //License from project: Open Source License public class Main { public static byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else/*from ww w. ja v a 2 s.co m*/ result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; } }