Java Utililty Methods Boolean to Byte

List of utility methods to do Boolean to Byte

Description

The list of methods to do Boolean to Byte are organized into topic(s).

Method

bytebooleanToByte(boolean b)
boolean To Byte
return b ? (byte) 1 : 0;
byteBooleanToByte(boolean value)
Converts a boolean into a single byte
if (value == true) {
    return 1;
return 0;
byte[]booleanToByte(boolean x)
boolean To Byte
if (x) {
    return new byte[] { (byte) 1 };
} else {
    return new byte[] { (byte) 0 };
bytebooleanToByte(boolean[] array)
boolean To Byte
byte val = 0;
for (boolean b : array) {
    val <<= 1;
    if (b)
        val |= 1;
return val;
byte[]booleanToByte(boolean[] bool)
Convert an array of booleans to bytes
byte[] byt = new byte[bool.length];
for (int i = 0; i < bool.length; i += 1) {
    byt[i] = bool[i] ? (byte) 'T' : (byte) 'F';
return byt;
byte[]booleanToByte(boolean[] values)
boolean To Byte
if (values == null) {
    return null;
byte[] results = new byte[values.length];
for (int i = 0; i < values.length; i++) {
    results[i] = (byte) (values[i] == true ? 1 : 0);
return results;
...
bytebooleanTobyte(final boolean value)
boolean Tobyte
return (byte) (value ? 1 : 0);
bytebooleanToByteBitflags(boolean[] flags)
boolean To Byte Bitflags
if (flags.length > 8)
    throw new IllegalArgumentException("You cannot store more than 8 bits on a byte!");
byte n = 0;
for (int i = 0; i < flags.length; i++) {
    if (flags[i])
        n += (1 << i);
return n;
...
byte[]booleanToBytes(boolean b)
boolean To Bytes
return new byte[] { (byte) (b ? 1 : 0) };
byte[]booleanToBytes(boolean b)
This function converts a boolean to its corresponding byte array format.
byte[] buffer = new byte[1];
booleanToBytes(b, buffer);
return buffer;