Connecting Tech Pros Worldwide Forums | Help | Site Map

How to combine TWO 32-bit words into one word.

Member
 
Join Date: Jul 2008
Posts: 62
#1: Dec 31 '08
Hi - I have a count register, which is made up of two 32-bit "words" - one for the high half of the value, and other for the lower 32 bits of the value.

What is the best way in C to combine these two values - I just want to stick them together so they are one large number, and then display them.

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Dec 31 '08

re: How to combine TWO 32-bit words into one word.


I understand ur question like this.
U have 2 int's (32 bit) and u want to combine both into a long long int (64 bit).

If yes then you have to put fthe first variable into long long int and then left shift it and then put the next int to the same long long int.


Raghu
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: Dec 31 '08

re: How to combine TWO 32-bit words into one word.


Option one: simply declare a single "long long" integer variable. That gives you a single 64-bit counter.

Option two: declare two "unsigned long" variables (highCount and lowCount). To increment your wide counter, increment just the lowCount unless it is about to overflow; in which case you increment the highCount and clear the lowCount. How do you know it is about to overflow? Look in <limits.h> for the macro for the largest value that fits in an unsigned long and compare against that. Printing the counter in hexadecimal is simple; printing it in decimal is a nightmare.

Option three: use a third-party library. Google "arbitrary precision arithmetic".
Reply