Since byte is an 8-bit byte
Therefore, it can be used to store boolean arrays with array 8, which are often used in communication protocols. Here is a Java code that converts it to each other.
package com.udpdemo.test2;import java.util.Arrays;public class Test {/** * @param args * */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(Byte.SIZE); byte b = 0x35; // 0011 0101 System.out.println(b); System.out.println(Arrays.toString(getBooleanArray(b))); //0x35; // 0011 0101 boolean[] array = new boolean[]{false, false, true, true, false, true, false, true}; System.out.println(getByte(array)); } /** * Convert byte to a boolean array of length 8 (each bit represents a boolean value) * * @param b byte * @return boolean array*/ public static boolean[] getBooleanArray(byte b) { boolean[] array = new boolean[8]; for (int i = 7; i >= 0; i--) { //Determine for each bit of byte array[i] = (b & 1) == 1; //Determine whether the last bit of byte is 1, if 1, it is true; otherwise it is false b = (byte) (b >> 1); //Transfer byte byte by one right} return array; } /** * Convert a boolean array of length 8 (each bit represents a boolean value) to byte * @param array * @return * */ public static byte getByte(boolean[] array) { if(array != null && array.length > 0) { byte b = 0; for(int i=0;i<=7;i++) { if(array[i]){ int nn=(1<<(7-i)); b += nn; } } return b; } return 0; } }The above brief discussion on the conversion of byte and boolean array with length 8 is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.