Skip to contentSkip to conversation

mdadm 2.5 (Was: ANNOUNCE: mdadm 2.5 - A tool for managing Soft RAID under Linux)

PATCH Alex Davis 4 participants 4 messages May 31, 2006 – Jun 1, 2006 MD RAID

lore
Share & tools
lore

Alex Davis

author
#0 lore raw
Quoted 10 lines

short swap16(short in)

{
       int i;
       short out=0;
       for (i=0; i<4; i++) {
               out = out<<8 | (in&255);
               in = in >> 8;
       }
       return out;
}

Shouldn't that be "for (i=0; i<2; i++) {..." ?

I code, therefore I am

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#1 lore raw
Quoted 11 lines · Alex Davis
Quoted 10 lines

short swap16(short in)

{
      int i;
      short out=0;
      for (i=0; i<4; i++) {
              out = out<<8 | (in&255);
              in = in >> 8;
      }
      return out;
}
Quoted 1 line

Shouldn't that be "for (i=0; i<2; i++) {..." ?

In which case, do we really need this complexity rather than a clear swap, e.g.

	return (short)(((in&0x0ff)<<8) | ((in>>8)&0x0ff))

Simple enough for a macro too.

Signature · 2 lines
Eyal Lebedinsky ([email protected]) <http://samba.org/eyal/>
	attach .zip as .dat
#2 lore raw
Quoted 12 lines · On Thursday June 1, [email protected] wrote:
Quoted 11 lines · Alex Davis
Quoted 10 lines

short swap16(short in)

{
      int i;
      short out=0;
      for (i=0; i<4; i++) {
              out = out<<8 | (in&255);
              in = in >> 8;
      }
      return out;
}
Quoted 1 line
Quoted 1 line

Shouldn't that be "for (i=0; i<2; i++) {..." ?

Well, yes... if you want the code to actually work, I guess that might be an improvement....

Quoted 4 lines

In which case, do we really need this complexity rather than a clear swap, e.g.

	return (short)(((in&0x0ff)<<8) | ((in>>8)&0x0ff))

What I like about the above is that the difference between swap16, swap32 and swap64 is just one number. In this case I think clarity and maintainability are much more important than efficiency.

Thanks for the comment,
NeilBrown

#3 lore raw
Quoted 13 lines · Neil Brown
Quoted 12 lines · On Thursday June 1, [email protected] wrote:
Quoted 11 lines · Alex Davis
Quoted 10 lines

short swap16(short in)

{
      int i;
      short out=0;
      for (i=0; i<4; i++) {
              out = out<<8 | (in&255);
              in = in >> 8;
      }
      return out;
}
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Shouldn't that be "for (i=0; i<2; i++) {..." ?

Well, yes... if you want the code to actually work, I guess that might be an improvement....

most important,
shouldn't that be:
__u16 swap16(__u16 in)

?

Regards,
L.

Signature · 6 lines
Luca Berra -- [email protected]
       Communication Media & Services S.r.l.
/"\
\ /     ASCII RIBBON CAMPAIGN
 X        AGAINST HTML MAIL
/ \