Shubhojit
Shubhojit

Reputation: 17

Java BigInteger.and doesn't work for big numbers?

I'm creating two huge BigIntegers and then anding them. But the and operation doesn't work and instead returns 0. Why?

BigInteger aa = new BigInteger("213092840173096182527577008347205670468257779233261101799142588416");
BigInteger bb = new BigInteger("226156424291633194186662097633113218007386784142018559245972777080014766080");
System.out.println(aa.and(bb));

Output: 0

I did a BigInteger 'or' on those two numbers and the 'or' operation worked fine. Anybody else see the same issue?

Please note that I am using jdk 1.8.

Upvotes: 1

Views: 118

Answers (2)

Shubhojit
Shubhojit

Reputation: 17

Resolved. The two numbers are decimal and then really and to zero.

Upvotes: 0

Programmer Person
Programmer Person

Reputation: 618

That is because their and is zero! They have no common bits.

Verified using python:

>>> x = 226156424291633194186662097633113218007386784142018559245972777080014766
080
>>> y = 213092840173096182527577008347205670468257779233261101799142588416
>>> x & y
0L
>>> bin(x)
'0b10000000000000000000000000000000000000000000000000000000000000000000000000000
00000001100000000000000000000000000000000000000100000000000000000000010010100000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000'
>>> bin(y)
'0b10000001100000000000000000000000000000000000000000000000000000110000000000000
00111000000001000000000000000000000000000000000000000000000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000'
>>>

btw, a general comment: before claiming a bug in well tested heavily used libraries, it is prudent to see if you are using it incorrectly/the results are as expected.

Upvotes: 5

Related Questions