Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.DataOutput; import java.io.IOException; public class Main { public static void writeBooleanArray(DataOutput out, boolean[] v, boolean extra) throws IOException { int len = v.length + 1; for (int chunk = 0; chunk < len; chunk += 8) { byte encoding = 0; for (int i = chunk; i < len && i < chunk + 8; i++) { encoding <<= 1; if (i == v.length) { encoding += extra ? 1 : 0; //v[len] is the extra piece } else { encoding += v[i] ? 1 : 0; } } out.writeByte(encoding); } } public static void writeBooleanArray(DataOutput out, boolean[] v) throws IOException { for (int chunk = 0; chunk < v.length; chunk += 8) { byte encoding = 0; for (int i = chunk; i < v.length && i < chunk + 8; i++) { encoding <<= 1; encoding += v[i] ? 1 : 0; } out.writeByte(encoding); } } }