Re: BitSet vs BigInteger (false Android doc)
On 9/5/2011 11:58 PM, Jan Burse wrote:
....
- BigInteger is also not dependent for positive values on some
two's complement, sign-plus-maginitude or one's complement etc..,
since these presentation were invented for negative values.
....
In dealing with the bit operations BigInteger does have to deal with the
issues of 2's complement. The API supports bit operations on negative,
as well as positive, BigInteger values, and requires 2's complement
behavior. For example, this program prints "-4":
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
BigInteger x = BigInteger.valueOf(-3);
BigInteger y = BigInteger.valueOf(-2);
System.out.println(x.and(y));
}
}
This program prints "-1", despite the positive inputs to the bit
manipulation.
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
BigInteger x = BigInteger.valueOf(3);
BigInteger y = BigInteger.valueOf(2);
System.out.println(x.or(y.not()));
}
}
I suppose, if they felt that the never-negative subset of the
BigInteger bit manipulations were particularly useful and important, the
developers could have done some special case optimization to avoid the
conversions. The combination of the comment in question and reading the
code shows that they did not take that path. Instead, they always
convert for bit manipulation and advise use of BitSet instead.
Patricia