Java Utililty Methods Byte to Bit

List of utility methods to do Byte to Bit

Description

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

Method

StringbyteToBit(byte b)
byte To Bit
return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1)
        + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
        + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
boolean[]byteToBitArray(byte b)
byte To Bit Array
boolean[] buff = new boolean[8];
int index = 0;
for (int i = 7; i >= 0; i--) {
    buff[index++] = ((b >>> i) & 1) == 1;
return buff;
StringbyteToBits(byte b)
byte To Bits
String bits = Integer.toBinaryString(b & 0xFF);
while (bits.length() < 8) {
    bits = "0" + bits;
return bits;
StringbyteToBits(byte b)
byte To Bits
StringBuffer buf = new StringBuffer();
int n = 256;
for (int i = 0; i < 8; i++) {
    n /= 2;
    if ((b & n) != 0)
        buf.append('1');
    else
        buf.append('0');
...
boolean[]byteToBits(byte b)
byte To Bits
boolean[] bits = new boolean[8];
for (int i = 0; i < 8; i++) {
    bits[7 - i] = ((b & (1 << i)) != 0);
return bits;
StringbyteToBits(byte b)
byte To Bits
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 8; i++)
    buf.append((int) (b >> (8 - (i + 1)) & 0x0001));
return buf.toString();
StringbyteToBits(byte b)
byte To Bits
return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1)
        + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
        + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
byte[]byteToBitsArray(byte b)
This method convert a single byte to a 8-bytes array.
byte[] result = new byte[8];
short val = (short) (b + 128);
for (int i = 0; i < 8; i++) {
    result[7 - i] = (byte) (val % 2);
    val /= 2;
return result;
StringbyteToBitStr(byte b)
byte To Bit Str
String zero = "00000000";
String s = Integer.toBinaryString(b);
return zero.substring(0, 8 - s.length()) + s;