Developer community
0 votes
273 views
asked by Sr Member (4.8k points) 10 25 31 | 273 views

1 Answer

0 votes
import java.util.BitSet;
public class Main {
  public static void main(String[] argv) throws Exception {
     System.out.println(fromByteArray(new byte[]{1,2,3}));
  }
 
  // Returns a bitset containing the values in bytes.
  public static BitSet fromByteArray(byte[] bytes) {
    BitSet bits = new BitSet();
    for (int i = 0; i < bytes.length * 8; i++) {
      if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
        bits.set(i);
      }
    }
    return bits;
  }
}
//{0, 1, 9, 16}
 
answered by Geek (15.3k points) 17 42 64

Related questions

0 votes
1 answer
asked by omari Sr Member (3.9k points) 5 8 11 | 12 views
+1 vote
1 answer
asked by john Guru (27.1k points) 33 51 102 | 43 views
0 votes
1 answer
asked by shivani Sr Member (1.1k points) 13 28 42 | 96 views
0 votes
1 answer
asked by john Guru (27.1k points) 33 51 102 | 42 views
0 votes
1 answer
asked by smith Geek (15.3k points) 17 42 64 | 70 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users