user3235117 user3235117 - 5 months ago 17
Java Question

Bit numbering - find first set (ffs) or find first one (ffo) Java

for (int i = 32; i <= 127; i++) {
}


I convert the number
int 32
into binary number
00100000
and the number
int 127
into binary
01111111
. I need the first position of one read from right (Bit numbering - find first set (ffs) or find first one (ffo)), 00100000 ->
6
and at 01111111 ->
1


Thank you!

Answer

The API is your friend too :

static int position(int a){
    int pos = Integer.numberOfTrailingZeros(a);
    return pos == 32 ? -1 : pos;
}