[ACCEPTED]-How to convert a byte into bits?-java

Accepted answer
Score: 11

If you just need the String representation 5 of it in binary you can simply use Integer.toString() with 4 the optional second parameter set to 2 for 3 binary.

To perform general bit twiddling 2 on any integral type, you have to use logical 1 and bitshift operators.

// tests if bit is set in value
boolean isSet(byte value, int bit){
   return (value&(1<<bit))!=0;
} 

// returns a byte with the required bit set
byte set(byte value, int bit){
   return value|(1<<bit);
}
Score: 5

You might find something along the lines 6 of what you're looking in the Guava Primitives package.

Alternatively, you 5 might want to write something like

public boolean[] convert(byte...bs) {
 boolean[] result = new boolean[Byte.SIZE*bs.length];
 int offset = 0;
 for (byte b : bs) {
  for (int i=0; i<Byte.SIZE; i++) result[i+offset] = (b >> i & 0x1) != 0x0;
  offset+=Byte.SIZE;
 }
 return result;
}

That's 4 not tested, but the idea is there. There 3 are also easy modifications to the loops/assignment 2 to return an array of something else (say, int or 1 long).

Score: 3

BitSet.valueOf(byte[] bytes)

You may have to take a look at the source 2 code how it's implemented if you are not 1 using java 7

Score: 2

Java has bitwise operators. See a tutorial example.

The Java programming language 18 also provides operators that perform bitwise 17 and bit shift operations on integral types. The 16 operators discussed in this section are 15 less commonly used. Therefore, their coverage 14 is brief; the intent is to simply make you 13 aware that these operators exist.

The unary 12 bitwise complement operator "~" inverts 11 a bit pattern; it can be applied to any 10 of the integral types, making every "0" a 9 "1" and every "1" a 8 "0". For example, a byte contains 7 8 bits; applying this operator to a value 6 whose bit pattern is "00000000" would 5 change its pattern to "11111111".

A 4 byte value IS integral, you can check bit state 3 using masking operations. The least significant 2 bit corresponds to the mask 1 or 0x1, the next 1 bit correspond to 0x2, etc.

byte b = 3;
if((b & 0x1) == 0x1) {
    // LSB is on
} else {
    // LSB is off
}
Score: 1
byte ar[] ;
byte b = ar[0];//you have 8 bits value here,if I understood your Question correctly :)

0

Score: 0

Well I think I get what you mean. Now a 4 rather substantial error with this is that 3 it doesn't work on negative numbers. However 2 assuming you're not using it to read file 1 inputs, you might still be able to use it.

public static ArrayList<Boolean> toBitArr(byte[] bytearr){
    ArrayList<Boolean> bitarr = new ArrayList<Boolean>();
    ArrayList<Boolean> temp = new ArrayList<Boolean>();
    int i = 0;
    for(byte b: bytearr){
        while(Math.abs(b) > 0){
            temp.add((b % 2) == 1);
            b = (byte) (b >> 1);
        }
        Collections.reverse(temp);
        bitarr.addAll(temp);
        temp.clear();
    }
    return bitarr;
}
Score: 0

Here is a sample, I hope it is useful for 1 you!

DatagramSocket socket = new DatagramSocket(6160, InetAddress.getByName("0.0.0.0"));
socket.setBroadcast(true);
while (true) {
    byte[] recvBuf = new byte[26];
    DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
    socket.receive(packet);
    String bitArray = toBitArray(recvBuf);
    System.out.println(Integer.parseInt(bitArray.substring(0, 8), 2)); // convert first byte binary to decimal
    System.out.println(Integer.parseInt(bitArray.substring(8, 16), 2)); // convert second byte binary to decimal
}
public static String toBitArray(byte[] byteArray) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            sb.append(String.format("%8s", Integer.toBinaryString(byteArray[i] & 0xFF)).replace(' ', '0'));
        }
        return sb.toString();
}

More Related questions