Android Boolean Array to Byte Array Convert createByteArr(boolean[] booleans)

Here you can find the source of createByteArr(boolean[] booleans)

Description

create Byte Arr

Declaration

public static byte[] createByteArr(boolean[] booleans) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] createByteArr(boolean[] booleans) {
        int length = booleans.length;
        byte[] bytes = new byte[length / 8 + 1];

        for (int i = 0; i < length; i++) {
            if (!booleans[i])
                continue;
            int word = i >>> 3;
            bytes[word] |= 1 << (i & 7);
        }/*from w w  w .j av  a2s.c  o m*/
        return bytes;
    }

    public static byte[] createByteArr(Boolean[] booleans) {
        int length = booleans.length;
        byte[] bytes = new byte[length / 8 + 1];

        for (int i = 0; i < length; i++) {
            if (!booleans[i])
                continue;
            int word = i >>> 3;
            bytes[word] |= 1 << (i & 7);
        }
        return bytes;
    }
}

Related

  1. createByteArr(Boolean[] booleans)