0

I m trying to convert 8 bits into one byte. The way the bits are represented are by using a byte object that only contains a 1 or a 0.

If i have a 8 length byte array with these bits, how can i convert them into one byte.

public byte bitsToByte(byte[] bits) {
 //Something in here. Each byte inside bits is either a 1 or a 0.
}

Can anyone help?

Thanks

3
  • Why not add the powers of two where there's a 1? It's just binary. Commented Dec 30, 2015 at 3:43
  • You want to convert byte to bits? Commented Dec 30, 2015 at 3:45
  • your each bit in itself a byte type, don't get confused between Java data types and pure meaning of bits and bytes. Do you mean concatenating those bits to get a single byte format? Commented Dec 30, 2015 at 3:52

2 Answers 2

3
public static byte bitsToByte(byte[] bits){
    byte b = 0;
    for (int i = 0; i < 8; i++)
        b |= (bits[i]&1) << i;
    return b;
}

//as an added bonus, the reverse.
public static byte[] bitsToByte(byte bits){
    byte[] b = new byte[8];
    for (int i = 0; i < 8; i++)
        b[i] = (byte) ((bits&(1 << i)) >>> i);
    return b;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Downvotes are good way to provide feedback but adding some words to it will make it a best way. IMHO
-1

left shift by 1 first for each bit in the array and then add the bit to the byte.

The implementation is based on the assumption that first bit in the array is sign bit and following bits are the magnitude in higher to lower positions of the byte.

public byte bitsToByte(byte[] bits) {
  byte value = 0;
  for (byte b : bits) {
    value <<=1;
    value += b;
  }
  return value;
}

Test the method:

public static void main(String[] args) {
  BitsToByte bitsToByte = new BitsToByte();
  byte bits[] = new byte[]{0,0,1,0,1,1,0,1};  // 1 + 0 + 4 + 8 + 0 + 32 + 0 + 0
  byte value = bitsToByte.bitsToByte(bits);
  System.out.println(value);
}

output:

45

Covert the byte array into a byte value (in the same order):

public static byte bitsToByte1(byte[] bits){
  byte result = 0;
  for (byte i = 0; i < bits.length; i++) {
    byte tmp = bits[i];
    tmp <<= i;             // Perform the left shift by "i" times. "i" position of the bit
    result |= tmp;         // perform the bit-wise OR 
  }
  return result;
}

input: (same array in reverse)

  byte bits1[] = new byte[]{1,0,1,1,0,1,0,0};
  value = bitsToByte1(bits1);
  System.out.println(value);

output:

45

3 Comments

Left shifting by one is just a multiplication by two; how will that add 4 and 8 to the byte? Wouldnt you left shift by several places to build the correct number?
If you are referencing 4 (third position from right) and 8 (forth position from right) from the above example then that is value represented by a bit in the byte. Yes. we will be doing the left shift each time we get a bit. I guess that what I am doing. I hope I got you correctly.
Downvotes are good way provide feedback but adding some words to it will make it a best way. IMHO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.