Converts a list of boolean values into its corresponding bit string. - Java java.lang

Java examples for java.lang:boolean

Description

Converts a list of boolean values into its corresponding bit string.

Demo Code


//package com.java2s;

import java.util.List;

public class Main {


    /**/*from  w w w .  j a  v  a  2  s .  co m*/
     * Converts a list of boolean values into its corresponding bit string.
     * e.g. {true, false, true, true, false} corresponds to the string:
     * "10110"
     *
     * @param bits List of boolean values
     * @return String of 1's and 0's corresponding to the binary representation of bits
     */
    public static String bitStringFromBooleanList(List<Boolean> bits) {
        String boolStr = "";
        for (Boolean bit : bits) {
            if (bit) {
                boolStr = boolStr.concat("1");
            } else {
                boolStr = boolStr.concat("0");
            }
        }
        return boolStr;
    }
}

Related Tutorials