import java.io.IOException;
import java.io.InputStream;
public class BinaryStream extends InputStream {
	
	private InputStream is = null;
	
	private int pos = -1;
	private int cur = 0;
	
	public BinaryStream(InputStream is) {
		this.is = is;
	}
	
	public int read() throws IOException {
		
		if (pos <= 0) {
			cur = is.read();
			if (cur == -1) {
				return -1;
			}
			pos = 8;
			while ((cur >> pos & 1) == 0) {
				pos--;
			}
		}
		else {
			pos--;
		}
		return cur >> pos & 1;
	}
}