Given the byte array:
{255, 3, 5}
which is equivalent to:
{11111111, 00000011, 00000101}
I’d like to get the following result:
{23,22,21,20,19,18,17,16, 9,8, 2,0}
Which is an array of the indices of 1’s in the input array.
What’s the fastest way of doing this in Java?
Update:
I’ve chosen the fastest solution, which @aioobe’s. Here are the test results of a pretty big data test:
@aioobe’s way:
35s 289ms
35s 991ms
36s 174ms
@Martijn’s way:
39s 274ms
39s 879ms
38s 684ms
Thanks you all! I appreciate your help.
Presumably by a 256 entry look-up table of the type
int[][]in whichlut[yourByte]equals the array of indexes for the ones inyourByte.You then just do something like