This is the best I can come up with given the API:
static boolean testBit(LargeInteger i, int n) {
return i.shiftRight(n).isOdd();
}
n
is the position of the bit to be tested.
I assume you put this method in some utility class.
Explanation
Normally, you would do num & (1 << pos)
to extract the bit at pos
position:
???????x?????
0000000100000
-------------
0000000x00000
If the whole thing is 0 then x
is 0; otherwise, x
is 1.
In the method above, I do num >> pos
:
???????x?????
-------------
????????????x
We know that a binary number is odd when its least significant bit is 1, and it is even when its least significant bit is 0.
So if the number after right-shifting is odd, we know the bit is 1; if even, we know the bit is 0.