Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Bytes' orde...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 30 Topic 9591 of 10094
Post > Topic >>

Re: Bytes' order with different C++ compilers

by Greg Herlihy <greghe@[EMAIL PROTECTED] > May 19, 2008 at 06:05 AM

On May 16, 5:27 pm, Alan McKenney <alan_mckenn...@[EMAIL PROTECTED]
> wrote:
> If a 32-bit integer is being sent in 4 bytes, msb first,
> there are two ways of reconstructing the value:
>
> A.   // only works if host is big-endian
>
>       unsigned char *buf = ....
>       unsigned long uint32value;
>
>       memcpy( &uint32value, buf+ipos, 4 );
>
> B.   // works regardles of endianness of your host
>
>       uint32value = ( buf[ipos]   << 24 ) |
>                     ( buf[ipos+1] << 16 ) |
>                     ( buf[ipos+2] <<  8 ) |
>                       buf[ipos+3] ;
>
> I generally use B, to avoid having to worry
> about endianness.

I generally would not use B, to avoid having to worry about poor
performance.

On the other hand, "A", although quite efficient, works only in big-
endian environments - and the goal here is for the source code to be
endian-agnostic.

So, based on the shortcomings of both A and B, it should be possible
to describe the optimal solution, "C". Solution C would combine the
endian-agnosticism of B, with the endian-dependent efficiency of A.
After all, it is not as if the endianness of the machine could ever
change while the program is running. So, although it makes sense to
write a single set of source code that is capable of executing in
either environment, it makes little sense to execute the same machine
code in both environments. There is nothing to be gained by spending
CPU cycles rearranging bytes - when it is certain that the bytes are
already in the proper order.

In fact, Nikolai's post on this thread demonstrates solution C: to
encapsulate endian-dependencies inside a function. For example, C++
programs on OS X are typically compiled twice - once for a big endian
architecture and again for a little endian architecture (and then the
two binaries are merged to create a "universal" binary capable or
running natively on either architecture). So, an OS X implementation
might look something like:

    unsigned int uint32value;

    stream >> uint32value;

    uint32alue = NXSwapBigIntToHost( uint32value );

NXSwapBigIntToHost() swaps uint32value's byte order only when this
source code is compiled for Intel architectures; when compiled for
PowerPC, NXSwapBigIntToHost() is compiled away to nothing. Of course,
the exact name of the byte-swapping routine is not im****tant in this
example - the im****tant point is that the program calls a routine to
(potentially) swap the bytes of the integer value - rather than to
swap the bytes itself.

Greg


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 30 Posts in Topic:
Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-11 16:40:44 
Re: Bytes' order with different C++ compilers
Carl Barron <cbarron41  2008-05-11 21:59:54 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-13 10:56:10 
Re: Bytes' order with different C++ compilers
Tony Delroy <tony_in_d  2008-05-13 21:14:12 
Re: Bytes' order with different C++ compilers
ThosRTanner <ttanner2@  2008-05-14 15:13:31 
Re: Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-14 15:13:17 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-14 18:10:42 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-15 11:17:54 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-15 16:04:27 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-16 17:48:11 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-16 18:27:29 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-17 02:29:20 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-17 14:35:47 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:56 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:45 
Re: Bytes' order with different C++ compilers
Greg Herlihy <greghe@[  2008-05-19 06:05:51 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-19 11:59:02 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-19 12:00:38 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-20 05:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-21 01:22:18 
Re: Bytes' order with different C++ compilers
"C. M. Heard" &  2008-05-21 01:28:17 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-21 14:03:42 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-21 14:01:51 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-21 14:27:32 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 01:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 14:58:37 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-24 09:28:08 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-25 09:57:35 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-26 16:16:35 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-28 10:41:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 8:07:05 CDT 2008.