Solved

Unsigned int64 to unsigned int32

Posted on 2013-01-13
10
1,807 Views
Last Modified: 2013-01-15
Fastest and efficient way to convert (unsigned) _uint64 to two (unsigned) _uint32

example

somefunc(in _uint64 , out _uint32, out _uint32)
0
Comment
Question by:Rahul Gupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 125 total points
ID: 38772088
Howdy....

The fastest way is to just let C do all of the work.

 _uint64 I64;
 _uint32 L32;
 _uint32 U32;

  L32 = I64;
  U32 = (I64 >> 32);

Depending on your compiler options you may wish to recast these items:

  L32 = (_uint32)I64;
  U32 = (_uint32)(I64 >> 32);


Good Luck,
Kent
0
 
LVL 4

Author Comment

by:Rahul Gupta
ID: 38772153
does it support both little endian and big endian systems
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38772159
Yep.  The C implementation is specifically mindful of the underlying hardware requirements and all integer type operations MUST work correctly according to the rules.

Now if you were to store the 64-bit value and try to extract it from the memory address and memory address+4 locations, all bets are off.  In this case you'd be at the mercy of the big/little endian rules.

But with integer operations the value is already converted to its "native" value in one of the hardware registers so it's simply a matter of selecting the upper or lower 32 bits.


Kent
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 4

Author Comment

by:Rahul Gupta
ID: 38773877
I had done in this way...

   _uint64= _uint64(_uint64(_uint32var1) << 32) | _uint32var2;

  _uint32var1 = _uint32(_uint64variable & &HFFFFFFFF);
  _uint32var2 = _uint32(_uint64variable >> 32);

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38774397
That's essentially the same.  The only significant different is that on line 3, you mask off the lower 32 bits of the 64 bit value before storing it in a 32-bit container.  The mask is unnecessary since the container can't possibly hold more than 32 bits.


Kent
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 125 total points
ID: 38774983
I use a bit of Delphi magic and do it like this:

procedure TfMain.FormDblClick(Sender: TObject);
type
  TDDWord = record
    Low, High : DWord;
  end;

var
  UI64 : UInt64;
  DDWord : TDDWord absolute UI64;

begin
  UI64 := 1;
  //DDWord.Low = 1
  //DDWord.High = 0
end;

Open in new window


By simply assigning the UI64 variable, the breakout of the two 32 bit pieces is done, with no extra code, no extra time.  The absolute keyword is for instances just like this, where you want to simply view one area of memory in a different way.  If you run this code in Delphi (with debug info on and optimizations off) then you will be able to inspect that the low value is 1 and the high value is 0, after you do the assignment.  The splitting litterally take no time...
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 38774985
If you need that solution to be mindful of different hardware then you would change the type with conditional compilation and it would be just as fast...
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 38776169
In C/C++ you can get the same effect by having a struct for the two smaller pieces.

struct Two32s {
  _uint32 Low;
  _uint32 High;
};

union 64BitSplit {
  _uint64 Whole;
  Two32s Split;
};

I haven't done C/C++ in a long time, so the code is "as is"... just to get the gyst of it.  If someone else feels the need to correct, by all means do!

If you assign to a variable of type 64BitSplit as

MyVar.Whole = VALUE;
You can then read out MyValue.Low and MyValue.High.  The Union makes the two structures occupy the same memory.

Once again, if the CPU independence is important then you should use conditional defines to make the struct swap low for high as needed.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38776216
That code will sometimes work, but it's hardware dependent.

It doesn't work for both big and little endian systems.  It should also have the appropriate #PRAGMA or command line option to force data alignment to the 32-bit boundary (or less).


Kent
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 38776682
Since you need to compile for little endian versus big endian, conditional compilation takes care of that... I thought I mentioned that already...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question