[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: 64bit ints and endianness
From: Shane Wright <me () shanewright ! co ! uk>
Date: 2002-06-24 21:13:26
[Download RAW message or body]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Sunday 23 June 2002 11:50 pm, Nicolas Goutte wrote:
> I am sorry, but as your sentence "doesn't byte 1 swap with byte 8, rather
> than byte 1 with byte 4 and byte 5 with byte 8?" can be understood in two
> opposite ways, I am not sure how to answer.
Sorry - I didn't explain that too well; but you've cunningly answered me below
;)
> But first I am wondering if your problem is not that you have not switched
> QDataStream to little-endian mode:
Thats all sweet - I'm only worrying about compatibility with big-endian
processors.
> However, yes, on a big-endian processor, you would need to swap the bytes
> as 1 to 8, 2 to 7...
Thats the one, Thanks!
> However, the idea is not to do explicit swapping in the code but to read
> the value byte per byte and to calculate the resulting value. At hardware
> or software level, the endianness swapping is done, if needed, while
> calculating the value (implicitely by the additions and multiplications.)
> As your problem is to read a little endian 64 bit value, you have the least
> significant byte first and the most significant last.
> You can read them in an array of chars:
> QDataStream stream;
> Q_INT8 a[8];
> for (int i=0;i<7;i++)
> stream>>a[i];
> Then you calculate:
> Q_INT64 value;
> value=a[7]<<56+a[6]<<48+a[5]<<40+a[4]<<32+a[3]<<24+a[2]<<16+a[1]<<8+a[0];
> That is also what the code below does. We just ask QT to do bytes 8 to 5
> and bytes 4 to 1 separately and then we calculated them together.
> Both codes work on big-endian and on little-endian machines to read a 64
> bit little endian value.
Ok, now I'm getting a little confused. So, if I copy that code above, it'll
work correctly for reading 64bit ints on both little-endian and big-endian
CPUs?
Aaahhh (light slowly comes on...), a[7] is the most significant byte so gets
left shifted, the host machine puts that in the right place and I end up with
a number the right way round! Got it (at last..)!
Thanks for your help, much appreciated :)
- --
Shane
http://www.shanewright.co.uk/
Public key: http://www.shanewright.co.uk/files/public_key.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9F4t45DXg6dCMBrQRAv2eAKDaa6pi8TgtBA0cCQTVuNN+h9gllgCaAhDr
Zkb0HSRRlzvM5JU4Ns2Gaow=
=qGmr
-----END PGP SIGNATURE-----
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic