package nl.nikhef.tools;

/**
 * Simple utility to manipulate bits in a byte array.
 * 
 * @author vincentb
 */
public final class Bits {
	
	private Bits() {
	}

	public static boolean get(byte[] bytes, int pos) 
	{
		if (pos < 0) return false;
		
		int byPos = pos / 8;
		int biPos = pos % 8;
		if (byPos >= bytes.length) return false;
		
		return ((bytes[byPos] >> biPos) & 0x1) != 0;
	}

	public static void update(byte[] bytes, int pos, boolean selected) {
		if (pos < 0) return;
		int byPos = pos / 8;
		int biPos = pos % 8;
		if (byPos >= bytes.length) return;
		if (selected)	bytes[byPos] |= 1 << biPos;
		else	 		bytes[byPos] &= ~ ( 1 << biPos );
	}
	
}
