Re: [Dev-C++] Convert Little-Endian to Big-Endian (and vice versa)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: William Mc C. <WIL...@ms...> - 2004-11-11 23:00:21
|
You might also want to look at using "exclusive OR" on each byte. ----- Original Message -----=20 From: Daniel K. O.<mailto:dan...@ya...>=20 To: Joey Dala<mailto:jda...@ya...>=20 Cc: = dev...@li...<mailto:dev...@li...= e.net>=20 Sent: Thursday, November 11, 2004 10:34 AM Subject: Re: [Dev-C++] Convert Little-Endian to Big-Endian (and vice = versa) Joey Dala wrote: > Does anybody know a faster algorithm to swap the bytes > of an 'unsigned long' than this: > =20 > // SWAP 32-BITS > #define SWAP_LONG( A ) ((A << 24) | ((A & 0xff00L) << 8 ) | ((A &=20 > 0xff0000L) >> 8 ) | ((unsigned long)A >> 24)) Faster than this? Only if you use architecture-specific instructions.=20 The 486 (and higher) family has the "bswap" instruction. Here's an = example: --- #include <stdio.h> #define SWAP32(x) asm volatile ("bswap %0" : "=3Dr" (x) : "0" = (x)) int main(void) { int foo =3D 1; =20 printf("%08x\n", foo); =20 asm volatile ( "bswap %0" : "=3Dr" (foo) : "0" (foo) ); =20 printf("%08x\n", foo); SWAP32(foo); printf("%08x\n", foo); getchar(); return 0; } --- Notice the use of the extended assembly syntax, so GCC can perform=20 better optimizations around the asm beast. Also, the "volatile" keyword to make sure GCC won't move the assembly=20 statement around in the optimization proccess. Before you ask, some information about using AT&T assembly syntax and=20 extended syntax is available here:=20 = http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html<ht= tp://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html> Daniel K. O. ------------------------------------------------------- This SF.Net email is sponsored by: Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux. = http://ads.osdn.com/?ad_id=3D5588&alloc_id=3D12065&op=3Dclick<http://ads.= osdn.com/?ad_id=3D5588&alloc_id=3D12065&op=3Dclick> _______________________________________________ Dev-cpp-users mailing list = Dev...@li...<mailto:Dev...@li...= e.net> TO UNSUBSCRIBE: = http://www23.brinkster.com/noicys/devcpp/ub.htm<http://www23.brinkster.co= m/noicys/devcpp/ub.htm> = https://lists.sourceforge.net/lists/listinfo/dev-cpp-users<https://lists.= sourceforge.net/lists/listinfo/dev-cpp-users> |