How to convert bytes to bits?

I have one byte array. I want to access each of the bytes and get its equivalent binary value (8 bits) in order to perform the following operations on it. I heard about BitSet, but is there any other way to handle this?

Thanks.

+7
java
source share
6 answers
byte ar[] ; byte b = ar[0];//you have 8 bits value here,if I understood your Question correctly :) 
+3
source share

If you just need a binary representation of a String, you can simply use Integer.toString() with the optional second parameter set to 2 for binary.

In order to perform the usual bit splitting by any integral type, you must use logical and bitwise 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); } 
+8
source share

You may find something similar to what you look in the Guava Primitives package.

Alternatively you can 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; } 

This is not tested, but there is an idea. There are also light changes in loops / assignments to return an array of something else (e.g. int or long ).

+4
source share

BitSet.valueOf (byte [] bytes)

You may need to take a look at the source code, how it is implemented if you are not using java 7

+3
source share

Java has bitwise operators . See case study .

The Java programming language also provides operators that perform bitwise and bit-wise operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the goal is to simply tell you that these operators exist.

The unary bitwise complement operator "~" inverts the bit pattern; it can be applied to any of the integral types, making each "0" a "1" and each "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit-bit “00000000” will change its pattern to “11111111”.

A byte IS integer value, you can check the status of bits using masking operations. The least significant bit corresponds to mask 1 or 0x1 , the next bit corresponds to 0x2 , etc.

 byte b = 3; if((b & 0x1) == 0x1) { // LSB is on } else { // LSB is off } 
+2
source share

Ok, I think I understand what you mean. Now, a pretty significant mistake is that it does not work with negative numbers. However, assuming that you are not using it to read input files, you can still 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; } 
0
source share

All Articles