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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    #1

    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.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    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

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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".

      Comment

      Working...