[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

How to Convert uint64 to uint32

Explore_Imagination

12/10/2008 4:17:00 PM

Hi All !!!

I am learning C++. Can any one guide me how we can efficiently convert
uint64 to unint32 ...

I am looking for an efficient way. E.g:

Let say I have a variable :

uint64 num = 6294967296;
Now I want to store value of "num" into two separate uint32
variables ???

Looking forward to hear from you guys.

/Hassan
37 Answers

S S

12/10/2008 5:22:00 PM

0

On Dec 10, 9:16 pm, Explore_Imagination <Mr.HassanShab...@gmail.com>
wrote:
> Hi All !!!
>
> I am learning C++. Can any one guide me how we can efficiently convert
> uint64 to unint32 ...
>
> I am looking for an efficient way. E.g:
>
> Let say I have a variable :
>
>                                         uint64 num = 6294967296;
> Now I want to store value of "num" into two separate uint32
> variables ???
>
> Looking forward to hear from you  guys.
>
> /Hassan

uint64 num = 6294967296;
uint32 lsb = num & 0xffffffff;
uint32 msb = num >> 32;

Andrey Tarasevich

12/10/2008 5:27:00 PM

0

Explore_Imagination wrote:
> I am learning C++. Can any one guide me how we can efficiently convert
> uint64 to unint32 ...
>
> I am looking for an efficient way. E.g:
>
> Let say I have a variable :
>
> uint64 num = 6294967296;
> Now I want to store value of "num" into two separate uint32
> variables ???

Assuming that 'uint64' and 'uint32' are just names for some integral
types... well, just store it

uint64 num = 6294967296;

uint32 a, b;
a = num;
b = num;

That's it. The value of 'num' is now stored in two two separate uint32
variables (assuming that it fits).

--
Best regards,
Andrey Tarasevich

red floyd

12/10/2008 6:01:00 PM

0

Explore_Imagination wrote:
> Hi All !!!
>
> I am learning C++. Can any one guide me how we can efficiently convert
> uint64 to unint32 ...
>
> I am looking for an efficient way. E.g:
>

If you are learning C++, then to hell with "efficiency". Learn to
write programs that are correct, and then PROFILE before optimizing.

Jeff Schwab

12/10/2008 6:26:00 PM

0

S S wrote:

> uint64 num = 6294967296;
> uint32 lsb = num & 0xffffffff;
> uint32 msb = num >> 32;

Does that assume a little-endian architecture?

Vaclav Haisman

12/10/2008 6:31:00 PM

0

Explore_Imagination wrote, On 10.12.2008 17:16:
> Hi All !!!
>
> I am learning C++. Can any one guide me how we can efficiently convert
> uint64 to unint32 ...
>
> I am looking for an efficient way. E.g:
>
> Let say I have a variable :
>
> uint64 num = 6294967296;
> Now I want to store value of "num" into two separate uint32
> variables ???
uint64 num = 6294967296;
uint32 hi, lo;
hi = num >> 32;
lo = num & 0xFFFFFFFF;

>
> Looking forward to hear from you guys.
>
> /Hassan

--
VH

peter koch

12/10/2008 6:56:00 PM

0

On 10 Dec., 19:25, Jeff Schwab <j...@schwabcenter.com> wrote:
> S S wrote:
> > uint64 num = 6294967296;
> > uint32 lsb = num & 0xffffffff;
> > uint32 msb = num >> 32;
>
> Does that assume a little-endian architecture?

No. Why do you think so? (It assumes 32-bit integers).

/Peter

S S

12/10/2008 7:13:00 PM

0

On Dec 10, 11:30 pm, Vaclav Haisman <v.hais...@sh.cvut.cz> wrote:
> Explore_Imagination wrote, On 10.12.2008 17:16:> Hi All !!!
>
> > I am learning C++. Can any one guide me how we can efficiently convert
> > uint64 to unint32 ...
>
> > I am looking for an efficient way. E.g:
>
> > Let say I have a variable :
>
> >                                         uint64 num = 6294967296;
> > Now I want to store value of "num" into two separate uint32
> > variables ???
>
> uint64 num = 6294967296;
> uint32 hi, lo;
> hi = num >> 32;
> lo = num & 0xFFFFFFFF;
>
>
>
> > Looking forward to hear from you  guys.
>
> > /Hassan
>
> --
> VH
>
>  signature.asc
> < 1KViewDownload

I thought the same but why not just,
lo = num
it will truncate itself the lower part?

Explore_Imagination

12/10/2008 7:31:00 PM

0

On Dec 10, 7:30 pm, Vaclav Haisman <v.hais...@sh.cvut.cz> wrote:
> Explore_Imagination wrote, On 10.12.2008 17:16:> Hi All !!!
>
> > I am learning C++. Can any one guide me how we can efficiently convert
> > uint64 to unint32 ...
>
> > I am looking for an efficient way. E.g:
>
> > Let say I have a variable :
>
> >                                         uint64 num = 6294967296;
> > Now I want to store value of "num" into two separate uint32
> > variables ???
>
> uint64 num = 6294967296;
> uint32 hi, lo;
> hi = num >> 32;
> lo = num & 0xFFFFFFFF;
>
>
>
> > Looking forward to hear from you  guys.
>
> > /Hassan
>
> --
> VH
>
>  signature.asc
> < 1KViewDownload

Can you explain each step ... what you are doing here ... why you have
used 0xFFFFFFFF ...
Also i have tried this solution but inorder to verify when I added lo
+hi it should be
equal to num like

lo+hi = num ... but your solution doesn't satisfy it

Jeff Schwab

12/10/2008 7:47:00 PM

0

peter koch wrote:
> On 10 Dec., 19:25, Jeff Schwab <j...@schwabcenter.com> wrote:
>> S S wrote:
>>> uint64 num = 6294967296;
>>> uint32 lsb = num & 0xffffffff;
>>> uint32 msb = num >> 32;
>> Does that assume a little-endian architecture?
>
> No. Why do you think so?

I don't.

> (It assumes 32-bit

unsigned

> integers).

Explore_Imagination

12/10/2008 7:50:00 PM

0

On Dec 10, 8:47 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> peter koch wrote:
> > On 10 Dec., 19:25, Jeff Schwab <j...@schwabcenter.com> wrote:
> >> S S wrote:
> >>> uint64 num = 6294967296;
> >>> uint32 lsb = num & 0xffffffff;
> >>> uint32 msb = num >> 32;
> >> Does that assume a little-endian architecture?
>
> > No. Why do you think so?
>
> I don't.
>
> > (It assumes 32-bit
>
> unsigned
>
> > integers).
>
>

Can you explain each step ... what you are doing here ... why you have
used 0xffffffff ...
Also i have tried this solution but inorder to verify when I added lo
+hi it should be
equal to num like

lo+hi = num ... but your solution doesn't satisfy it