集册 Java实例教程 将布尔数组转换为字节。

将布尔数组转换为字节。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

771
将布尔数组转换为字节。
/** 来 自 n o w  j a v a  . c o m**/

//package com.nowjava;


public class Main {

    /**

     * Converys a boolean array into a byte. Each boolean refers to a different digit in the byte's binary. For example, a boolean

     * array of [true, true, true, false, false, false, true, false] would return a byte with the binary makeup of 11100010. (Or -98)

     * @param a - The boolean array to convert. Must be a length of at least 8.

     * @return The byte.

     */

    public static byte fromBooleanArray(boolean[] a) {

        byte b = 0;

        if (a[7])

            b += 1;

        if (a[6])

            b += 2;

        if (a[5])

            b += 4;

        if (a[4])

            b += 8;

        if (a[3])
        /**
        时 代 J a v a 公 众 号 - nowjava.com
        **/

            b += 16;

        
展开阅读全文