Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 30th, 2008, 11:37 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default How to combine TWO 32-bit words into one word.

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.
Reply
  #2  
Old December 31st, 2008, 12:20 AM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 30
Posts: 1,247
Default

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
Reply
  #3  
Old December 31st, 2008, 01:58 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Age: 52
Posts: 571
Default

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.