Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes BigIntegers... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigIntegers..." Watch "BigIntegers..." New topic
Author

BigIntegers...

Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
I've spent over an hour on this.

I'm trying to add two BigIntegers and have some kind of knot somewhere. Can someone please help me add 1 + 1 using the BitInteger Object, then add A third one to the result of 1 + 1?

I feel so dumb.


Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Instances of BigInteger are objects, so regular numeric operators won't work. Instead, you call methods, defined in the API...

http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html

For example, to add existing BigInteger instances b1 and b2, then assign the result to b3, you could do the following...

BigInteger b3 = b1.add(b2);

Note that the sum is returned by the method, which does not change the values of b1 or b2.

Also note that "1" is a special constant, so you don't need to create your own instance. You can simply use BigInteger.ONE.
[ February 18, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Here you can find "short code samples" for operations whic involve BigIntegers :

Java Almanac

Also try to search for other "code samples" before posting your question here, I found many answers there. I hope it will help you

kind regards
Igor
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Thanks.

If I hadn't spent the time on it, I wouldn't have asked. I usually search here and Google befor I come for assistance.
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
I am not sure if You did actually read information provided by that link since its very straightforward and as i can see you have sun certificate so I asume you should be able to get your code working from this example which is provided by above link




kind regards
Igor
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You are a certified SCJP and you don't know how to look up this information in the Java API documentation? That just scares me...

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BigIntegers...
 
Similar Threads
Finding the largest Prime Factor
BigInterger math and conversions
sum of two int arrays
API Test question
Effective Java 2nd Edition - item 15