I need to find the highest order 1 in some longs, ints and shorts in Java. For example, if I had a char that looked like 00110101 , I need a method that will return 2 (the highest order index is 1).
Now I know that you can do this using a for loop, for example:
for(int i=0; i<8; i++) if((x & 1<<i) != 0) return i; return -1;
but it is slower than I want. I know that modern processors have instructions that do this on a chip, so I want to know how I can make a call, and not have an explicit loop.
EDIT: bonus points if you can just return the indices of all of them in the primitive.
Thanks.
java bit-manipulation
twolfe18
source share