Thread: 40 digit integer arithmetic (arrays)

1. 40 digit integer arithmetic (arrays)

what is the best way to deal with negative integers?

what I did is to string the integer. store each digit in an int array[40]. in case of a negative integer, the only negative digit will be the one with the highest place value.

when operating, say addition, i have to add the integer digit by digit. like doing it in paper. i got it when the integer is unsigned but im still having trouble when its signed. can you give me an idea how to tackle the negative integer arithmetic.

its just addition, subtraction and multiplication.

2. When you say you're only storing the sign on the highest place value, you mean the first number always, or the first nonzero digit (reading from the left)? If the former, you could be losing your signs, as -0 == 0. If the latter, that's nice for printing, but you'll have to find that sign before you try to add (since adding a negative is much like subtraction, etc.)

3. the first non zero digit is signed.

I store the ones place on index 0.

4. i need this by monday

if you can just give me some suggestions i'd really appreciate it

5. Do you know how two's complement representation works? If you're storing your numbers internally as binary that might be one solution (but I suspect you're just storing decimal digits here).

To code subtraction the long-hand way you'll need to implement borrowing. You may be interested in this: Subtraction algorithm

6. I think the subtraction algorithm wont work. The integer size is too large.

I'll try two's compliment

7. Originally Posted by freddyvorhees
I think the subtraction algorithm wont work. The integer size is too large.

I'll try two's compliment
That makes no sense. The subtraction algorithm is not size-restricted.

8. So, you read lets say -23213812381..312312 and 32131231..123312 and you want to do opereations on them? If that is the case I guess you read char by char. So why not put on array[0] the sign? For *, -, +, / just do it as you do o paper. Don't really see the problem.