Sum 0f 2 64 bits using 32 bits

Hi,
the question is you have cpu that knows to do
computations on 32 bits(unsigned integer).
write a function that find the sum of 2
64 bits numbers
using this cpu?
there is a struct:
typedef struct{
int low;(32 bits)
int high;(32 bits)
}64bit;

Questions by eminemm

Showing Answers 1 - 3 of 3 Answers

I am also looking for the answer of the same. I know how to do this in assembly, but I dont know how it works in C. I will try to explain how I do it in assembly, may be there is some same method for C.

Suppose be have two 64 bits number in variable A and B. The variable is subdevided in to AH:AL and BH:BL. (AL,AH,BL,BH are all 32 bits). Answer is stored in C (CH:CL)

1) so now first add AL and BL. so CL = AL+BL
2) if there is any carry than add it to higher byte. if no carry thn just add higher bytes.
CH = AH + BH + Carry(if any)

So your answer is in CH:CL....

We can check for the carry using the carry flag in assembly language. But how do you know if there is any carry or overflow in C??

  Was this answer useful?  Yes

amodiahs

  • Jul 24th, 2008
 

Hello friends,
Here we need to use a linked list. See, it is already known that CPU doesnot support one single 64bit number hence, first thing is that using normal data types will be useless. So here what we can do is.. Create two linked lists where each one will represent a 64bit # where each node holds a digit of the #. Now traversing these linked lists from tail to head we can keep on adding the digits of two nodes and keep the sum in a new linked list's node taking the carry # for next addition... so like this we can get a new linked list whihc wil contain the sum using which we can then make up a new 64bit structure as u have shown in ur question....

hope this logic helps.....

devendra

  • Aug 17th, 2011
 

well this can be done in any number of ways... One simple way is to use asm keyword and write assembly instructions if you are familiar with assembly language.. Because through assembly language we can have direct access to registers..

Another method is using linked list. One more method is using c and accessing overflow bit.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions