Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > swapping bytes in an integer

Reply
Thread Tools

swapping bytes in an integer

 
 
Rod Pemberton
Guest
Posts: n/a
 
      05-12-2006

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Rod Pemberton" <(E-Mail Removed)> writes:
> > "Keith Thompson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...

> [...]
> >> I didn't say they're non-rare; that's not the point. The point is
> >> that saying "rarely it's non-portable" merely obfuscates the fact that
> >> it's *non-portable*.

> >
> > Your perspective is what's incorrect. It is portable. It's
> > portable to any machine which uses 32-bits for 'unsigned long' which
> > is the extreme majority of architectures in existence. It's _rare_
> > for an 'unsigned long' to be any other size even on 16-bit or 64-bit
> > machines. You've attempted to redefine portability in terms of a
> > few obscure machines.

>
> 64-bit longs are not rare. 64-bit machines are not obscure. On
> 64-bit machines, 64-bit longs are much more common than 32-bit longs.
>
> >> 64-bit systems are already showing up as home PCs for everyday use,
> >> and they're becoming more and more common.
> >>
> >> If you write code that works only on 32-bit systems, it's likely to
> >> work on *most* modern systems. But why waste your time writing code
> >> that works on most systems, when you can write *portable* code with
> >> little additional effort?

> >
> > Since you (claim to) have experience in this area, feel free to post a
> > portable solution. But, that would violate your personal no-code policy
> > wouldn't it?

>
> Stop trolling, and stop making things up.
>


Unjustified insults. If you wish to claim that my code is non-portable,
that's fine. But, at least back up your argument by teaching the OP, me,
and everyone else here how to write portable code by actually posting some
that you wrote.


Rod Pemberton


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-12-2006
"Rod Pemberton" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Rod Pemberton" <(E-Mail Removed)> writes:

[...]
>> > Since you (claim to) have experience in this area, feel free to post a
>> > portable solution. But, that would violate your personal no-code policy
>> > wouldn't it?

>>
>> Stop trolling, and stop making things up.

>
> Unjustified insults.


Not at all. You claim I have a "personal no-code policy". I do not.
You made it up.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Rod Pemberton
Guest
Posts: n/a
 
      05-12-2006

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Rod Pemberton" <(E-Mail Removed)> writes:
> > "Keith Thompson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> "Rod Pemberton" <(E-Mail Removed)> writes:

> [...]
> >> > Since you (claim to) have experience in this area, feel free to post

a
> >> > portable solution. But, that would violate your personal no-code

policy
> >> > wouldn't it?
> >>
> >> Stop trolling, and stop making things up.

> >
> > Unjustified insults.

>
> Not at all. You claim I have a "personal no-code policy". I do not.
> You made it up.
>


Really? (I have yet to see one since I've been posting here, since I've been
reading here-alot longer, in Google's indexing-even longer...) So perhaps
your policy is implicit instead of explicit. Put some money where your
mouth is: post some code that you wrote. A good choice would be to the OP's
question.


Rod Pemberton


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-13-2006
Rod Pemberton wrote:
>
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "Rod Pemberton" <(E-Mail Removed)> writes:
> > > "Keith Thompson" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > >> "Rod Pemberton" <(E-Mail Removed)> writes:


> > >> > feel free to post a portable solution.


> > >> > A good choice would be to the OP's question.


To use OP's example value portably,
the type has to be at least as large as long.

/* BEGIN new.c */

#include <stdio.h>

long unsigned rev_uint_bytes(long unsigned number);

int main(void)
{
long unsigned x = 0x0c0b0a09;

printf("x is %lx\n", x);
x = rev_uint_bytes(x);
printf("x with the byte order reversed is %lx\n", x);
return 0;
}

long unsigned rev_uint_bytes(long unsigned number)
{
unsigned char *lower, *upper, swap;

lower = (unsigned char*)&number;
upper = lower + sizeof number;
do {
swap = *lower;
*lower++ = *upper;
*upper-- = swap;
} while(upper > lower);
return number;
}

/* END new.c */


--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-13-2006
pete wrote:
>
> Rod Pemberton wrote:
> >
> > "Keith Thompson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > "Rod Pemberton" <(E-Mail Removed)> writes:
> > > > "Keith Thompson" <(E-Mail Removed)> wrote in message
> > > > news:(E-Mail Removed)...
> > > >> "Rod Pemberton" <(E-Mail Removed)> writes:

>
> > > >> > feel free to post a portable solution.

>
> > > >> > A good choice would be to the OP's question.

>
> To use OP's example value portably,
> the type has to be at least as large as long.


> long unsigned rev_uint_bytes(long unsigned number)
> {
> unsigned char *lower, *upper, swap;
>
> lower = (unsigned char*)&number;
> upper = lower + sizeof number;
> do {
> swap = *lower;
> *lower++ = *upper;
> *upper-- = swap;
> } while(upper > lower);
> return number;
> }


I screwed that up.
It should be:

long unsigned rev_uint_bytes(long unsigned number)
{
unsigned char *lower, *upper, swap;

lower = (unsigned char*)&number;
upper = lower + sizeof number - 1;
while(upper > lower) {
swap = *lower;
*lower++ = *upper;
*upper-- = swap;
}
return number;
}

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
4-bytes or 8-bytes alignment? mrby C Programming 8 11-02-2004 08:45 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM
Re: receiving Bytes and sending Bytes Ieuan Adams Computer Support 0 07-24-2003 07:46 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments