TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: John Richardson
from: George White
date: 1998-08-27 08:31:02
subject: Big Vs Little Endian

Hi John,

JR> > This (down below) will not work on all machines. Fastest way of doing
JR> > it that I can think of without using endian swapping assembly
JR> > instructions.

JR>I think bit shifting is probably faster and is portable.

JR>#define Intel16(x)   ((x&0xff)>8)
JR>#define Intel32(x)
JR>((x&0xff)>8)+((x&0xff000000)>>24)
...........................................^^

But potentially dangerous as written :-(. There is also "00)" missing
from the second one :-(. The Intel32() one will give incorrect results
on a 16 bit compiler (even if the compiler warnings about "Constant is
long" are fixed).

I would write them as:

#define Intel16(x)   (((x)&0xff)>8)
#define Intel32(x) \
(((x)&0xffL)<<24)+(((x)&0xff00L)<<8) \
+(((x)&0xff0000L)>>8)+(((x)&0xff000000L)>>24)

Or, if you feel that way, "+" can be replaced by "|"... :-)

This will give correct operation if "x" is an expression including any
bitwise operators (&,^,|), as & has the highest precedence of the
bitwise operators. Adding the 'L's to force all the constants to "long"
in Intel32() allows the macros to work on 16 bit compilers (yes, I still
use one for embedded systems work...).

JR>No doubt Intel programmers will want to change the name of
JR>the macro to Motorola. :)

:-)

George

 * SLMR 2.1a * Computers eliminate spare time.

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717904) (2:440/4)
SEEN-BY: 396/1 622/419 632/0 371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 440/4 255/1 251/25 2320/38 270/101 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.