TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bo Simonsen
from: Jasen Betts
date: 2004-01-28 07:31:32
subject: Re: Big endian machines

Hi Bo.

22-Jan-04 15:50:10, Bo Simonsen wrote to Jasen Betts

 BS> Yes, but I guess there is still no problem in reading a aligned
 BS> structure (see the post from Pascal regarding this subject).

 JB>> &define  fido_swab(x)   (x=x<<8 & 0xff00 |
x>>8 & 0xff )

 BS> I'm not supposed to understand this right? ;-)

 > do bit shifting

it shifts the bits in the binary cavue across N places.

 >8 means /256

    some compilers may produce more efficient compiled object if they see
    bit shifts than if they see a multiplication or a division.

  & is bitwise and, it's used to mask out any unwanted bits
  | is bitwise or used to combine the two shifted bytes back together.

 JB>> { short int x=0 ((char*)x)[0]=1; fido_swab[x]

 BS> fido_swab(x) ?

oops, yes.

 JB>> if(x != 1) { fpriintf(stderr "fido_swab mis-defined in source";
 JB>> exit(1); } }

 JB>> This isn't a truly general solution as it won;'t work on macines
 JB>> that don't use two's complement numbers but those are
 JB>> practiacally unheard of these days,

 BS> It might be more portable to use the swap thing, because htonl
 BS> (and brothers) might not exist in some OS'es like Solaris(?).

htonl is a BSD thing, I'd expect it will exist in Solaris.. (linux has it)

 JB>> the other fix is to re-write it to use all bytes.

 BS>> struct _pkthdr { uchar orig_node_minor,orig_node_major;     /*
 BS>> originating node */ uchar dest_node_minor,dest_node_major;     /*
 BS>> destination node */ }  __attribute__((packed, aligned(1)));

 JB>> but then you may need to do more edits to your code so that it
 JB>> can get the node number

 JB>> eg:

 JB>> unsigned short orig_node = (pkt.orig_node_major << 8 & 0xff00)
 JB>> +(pkt.orig_node_minor      & 0x00FF)

 BS> Seems like a complicated one, I mean I shouldn't do swapping on a
 BS> single byte?

this is not actually swapping the bytes around it's only combining two bytes
from one location to make a 16-bit integer in a different loction

when it becomes time to write the bytes the reverse it done to break the
integer in to bytes so they can be written etc...

 -=> Bye <=-

---
* Origin: I smell a rat. Did you bake it or fry it? (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/2000 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™.