473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

swapping bytes in an integer

If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

Thanks,
-John

May 8 '06 #1
34 13842
On 8 May 2006 14:03:08 -0700, jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

Thanks,
-John


The shift operators << and >> shift a value left and right by one bit,
respectively. Shifting by 4 bits will move the value by one hex digit.
You can combine values with the bitwise OR operator, |
May 8 '06 #2
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.
May 8 '06 #3
Monkey monkey = 0x090a0b0c;

Opps! Should have written:
Monkey monkey;

monkey.four_bytes = 0x090a0b0c;
-Tomás
May 8 '06 #4
On Mon, 08 May 2006 21:35:03 GMT, "Tomás" <NU**@NULL.NULL> wrote:
Monkey monkey = 0x090a0b0c;

Opps! Should have written:
Monkey monkey;

monkey.four_bytes = 0x090a0b0c;
-Tomás


It makes no difference - we have no idea what Monkey is anyway. It was
probably simpler the first way, we could just assume it was an integer
of some description.
May 8 '06 #5
W Marsh <wa*********@gmail.com> writes:
On 8 May 2006 14:03:08 -0700, jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


The shift operators << and >> shift a value left and right by one bit,
respectively. Shifting by 4 bits will move the value by one hex digit.
You can combine values with the bitwise OR operator, |


No, the shift operators shift a value by a specified number of bits.
For example, x<<1 yields x shifted left by one bit, x<<4 yields x
shifted left by 4 bits, and x<<N yields x shifted left by N bits.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
May 8 '06 #6
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.


If I understand correctly, ntohl() converts a big endian integer to a
system integer, meaning if system integers are already big endian, it
simply returns whatever you pass it. So it may not be useful even when
it does exist.

Treating the number when it has the wrong byte order as an array of
unsigned char, and manually combining octets (or manually splitting the
number into them, if you have to go the other way), is a more portable
alternative, which if done right should work on any system.

May 8 '06 #7
On Mon, 08 May 2006 21:53:59 GMT, Keith Thompson <ks***@mib.org>
wrote:
W Marsh <wa*********@gmail.com> writes:
On 8 May 2006 14:03:08 -0700, jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


The shift operators << and >> shift a value left and right by one bit,
respectively. Shifting by 4 bits will move the value by one hex digit.
You can combine values with the bitwise OR operator, |


No, the shift operators shift a value by a specified number of bits.
For example, x<<1 yields x shifted left by one bit, x<<4 yields x
shifted left by 4 bits, and x<<N yields x shifted left by N bits.


I meant to write "shift a value left and right through bits", yes.
May 8 '06 #8
Harald van Dijk wrote:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

If I understand correctly, ntohl() converts a big endian integer to a
system integer, meaning if system integers are already big endian, it
simply returns whatever you pass it. So it may not be useful even when
it does exist.

Yes, you are correct. My advice was wrong.

--
Ian Collins.
May 8 '06 #9
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


Depends on whether you want to convert it in place or not. Anyway, the
simple way to understand is using shift operators >> and << and bitwise
and & for masking.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 8 '06 #10
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.


I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.

-John

May 8 '06 #11
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.edu wrote:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
> If I have a 32 bit unsigned int that is in the wrong byte order, how
> can I convert it? For example, if have a number 0x090a0b0c how can I
> reverse this to 0x0c0b0a09 ?
>

It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.


I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.

-John


Then bear in mind that it's not portable, and will have no effect when
compiled for a machine with a different endianess to your own.
May 8 '06 #12
On 2006-05-08, W Marsh <wa*********@gmail.com> wrote:
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.edu wrote:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
> If I have a 32 bit unsigned int that is in the wrong byte order, how
> can I convert it? For example, if have a number 0x090a0b0c how can I
> reverse this to 0x0c0b0a09 ?
>
It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.


I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.

-John


Then bear in mind that it's not portable, and will have no effect when
compiled for a machine with a different endianess to your own.


If his code depends on big-endianness, then it doesn't matter that the function
has no effect; it simply means that the machine is already set up correctly
for your functions.

I worded that really poorly, but basically I mean that it is irrelevant what
effect a certain function actually has; only the end result is important.
May 8 '06 #13
On Mon, 08 May 2006 22:41:34 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
On 2006-05-08, W Marsh <wa*********@gmail.com> wrote:
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.edu wrote:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
> If I have a 32 bit unsigned int that is in the wrong byte order, how
> can I convert it? For example, if have a number 0x090a0b0c how can I
> reverse this to 0x0c0b0a09 ?
>
It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.

I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.

-John


Then bear in mind that it's not portable, and will have no effect when
compiled for a machine with a different endianess to your own.


If his code depends on big-endianness, then it doesn't matter that the function
has no effect; it simply means that the machine is already set up correctly
for your functions.

I worded that really poorly, but basically I mean that it is irrelevant what
effect a certain function actually has; only the end result is important.


No - he asked how to swap bytes, not how to manifest a specific
endianess.

If he has data he wants to treat natively, then ntohl is fine.

If he actually wants to swap the bytes of an integer in any case
(implementing a swap opcode in a CPU emulator, for example), he won't
be able to use ntohl.
May 8 '06 #14
> If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


The following article should help:
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

May 10 '06 #15
EventHelix.com wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?
The following article should help:
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

From this web site...


Routines to convert between big-endian and little-endian formats are
actually quite straight forward. The routines shown below will convert
from both ways, i.e. big-endian to little-endian and back.
Big-endian to Little-endian conversion and back
short convert_short(short in)
{
short out;
char *p_in = (char *) &in;
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}

long convert_long(long in)
{
long out;
char *p_in = (char *) &in;
char *p_out = (char *) &out;
p_out[0] = p_in[3];
p_out[1] = p_in[2];
p_out[2] = p_in[1];
p_out[3] = p_in[0];
return out;
}

May 10 '06 #16

<jo**@fcs.uga.edu> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


It seems you've found ntohl(), but this is how:

unsigned long a,b;

a=0x090a0b0c;
b=((a&0xFF000000)>>24)|((a&0x00FF0000)>>8)|((a&0x0 000FF00)<<8)|((a&0x000000F
F)<<24);

Of course, you can drop any place holder zeros between 0x and FF.
Rod Pemberton
May 10 '06 #17


jo**@fcs.uga.edu wrote On 05/08/06 18:18,:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.

--
Ian Collins.

I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.


<off-topic reason="potential bug">

Be careful: ntohl() does *not* do what you asked.
It converts "network byte order" (Big-Endian) to "host
byte order" (whatever your machine uses). If your
machine uses Big-Endian byte order already, ntohl()
will not swap the bytes: it will do nothing at all.
To put it another way, ntohl() may in fact do what you
want on the machine you're using at the moment, but
will definitely *not* do what you want on all machines.

If you need to swap the bytes unconditionally, no
matter what machine you're using, you'll have to work
a little harder.

</off-topic>

--
Er*********@sun.com

May 10 '06 #18
Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty. Do you think that it could it be placed
within a #define ?

Thanks,
-John

May 10 '06 #19
Eric Sosman <Er*********@sun.com> writes:
jo**@fcs.uga.edu wrote On 05/08/06 18:18,:
Ian Collins wrote:
jo**@fcs.uga.edu wrote:
If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

It my be non-standard, bit if you system has the networking library
function ntohl and you don't require portability, you can use that.
I just tried ntohl() in my program and this works perfectly. I think I
will use this method. I want to thank everyone who replied for all of
their great ideas! This help is greatly appreciated.


<off-topic reason="potential bug">

Be careful: ntohl() does *not* do what you asked.
It converts "network byte order" (Big-Endian) to "host
byte order" (whatever your machine uses). If your
machine uses Big-Endian byte order already, ntohl()
will not swap the bytes: it will do nothing at all.
To put it another way, ntohl() may in fact do what you
want on the machine you're using at the moment, but
will definitely *not* do what you want on all machines.

If you need to swap the bytes unconditionally, no
matter what machine you're using, you'll have to work
a little harder.


We're not certain exactly what the OP's requirements are, only what he
stated them to be. He might need a general solution to swapping bytes
(in which case ntohl() won't do it), or it may be that the only time
he'll see a number in the wrong byte order is when it comes from a
network (in which case ntohl() is probably just the thing).
</off-topic>


--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
May 10 '06 #20

<jo**@fcs.uga.edu> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty. Do you think that it could it be placed
within a #define ?


Yes. Just remember that it's only for 'unsigned long' 's which are 32-bits
in length (i.e., rarely it's non-portable). You'd need a different version
for other unsigned types: short, long long, etc. This method won't work
with signed types.

/* BS for byte-swap */
#define BS(a)
(((a)&0xFF000000)>>24)|(((a)&0xFF0000)>>8)|(((a)&0 xFF00)<<8)|(((a)&0xFF)<<24
)

/*...*/
unsigned long a,b;

a=0x090a0b0c;
b=BS(a);
Each 'FF' masks off one of the four respective bytes using the '&' bitwise
and operator. This leaves you with four unsigned long each of which has a
byte to be relocated. The '>>' and '<<' bitshift operators move each byte
to it's new position (within an 'unsigned long'). The '|' bitwise or
operator puts all four unsigned long's, each one with one relocated byte,
back together.

1) four unsigned long's (each 'a')
0x090a0b0
0x090a0b0
0x090a0b0
0x090a0b0

2) and masking, four bytes within unsigned longs
0x09000000
0x000a0000
0x00000b00
0x0000000c

3) shifting, relocated byte within unsigned longs
0x00000009
0x00000a00
0x000b0000
0x0c000000

4) or four together
0x0c0b0a09
This actually optimizes very well with most modern compilers.

It also avoids using byte swapping constructs like arrays or unions which
(in my experience) can cause other programming headaches.
Rod Pemberton
May 10 '06 #21
On 9 May 2006 08:53:26 -0700,
jo**@fcs.uga.edu <jo**@fcs.uga.edu> wrote
in Msg. <11**********************@j33g2000cwa.googlegroups .com>
Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty.
It looks confusing at first, but is simple in reality.
Do you think that it could it be placed
within a #define ?


It could be, but the argument is evaluated four times which will create
trouble if it has side effects. If your macros is called BYTESWAP(a),
then

BYTESWAP(++a) will invoke undefine behavior, and
BYTESWAP(some_func(x)) will invoke some_func four times.

Better implement it as an (inline) function.

robert
May 10 '06 #22
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
<jo**@fcs.uga.edu> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty. Do you think that it could it be placed
within a #define ?


Yes. Just remember that it's only for 'unsigned long' 's which are 32-bits
in length (i.e., rarely it's non-portable). You'd need a different version
for other unsigned types: short, long long, etc. This method won't work
with signed types.


I think what he means by "rarely it's non-portable" is "it's
non-portable". unsigned long isn't necessary 32 bits; I regularly use
systems on which it's 64 bits.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
May 10 '06 #23

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
<jo**@fcs.uga.edu> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty. Do you think that it could it be placed
within a #define ?


Yes. Just remember that it's only for 'unsigned long' 's which are 32-bits in length (i.e., rarely it's non-portable). You'd need a different version for other unsigned types: short, long long, etc. This method won't work
with signed types.


I think what he means by "rarely it's non-portable" is "it's
non-portable". unsigned long isn't necessary 32 bits; I regularly use
systems on which it's 64 bits.


Just because you use a system regularly doesn't mean that it's a non-rare
system. I wouldn't exactly call the Sun-Blade-100's or Cray's you've used
common... In fact, I wouldn't call any mainframe or mini-frame (including
the ones I've used) common.
Rod Pemberton
May 10 '06 #24
Rod Pemberton wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
<jo**@fcs.uga.edu> wrote in message
news:11**********************@j33g2000cwa.googl egroups.com...

Wow! This code is really awesome. I think this is the absolute best
way I have seen yet. I will have to study your code and break it down
to see how it works exaclty. Do you think that it could it be placed
within a #define ?
Yes. Just remember that it's only for 'unsigned long' 's which are
32-bits
in length (i.e., rarely it's non-portable). You'd need a different
version
for other unsigned types: short, long long, etc. This method won't work
with signed types.


I think what he means by "rarely it's non-portable" is "it's
non-portable". unsigned long isn't necessary 32 bits; I regularly use
systems on which it's 64 bits.

Just because you use a system regularly doesn't mean that it's a non-rare
system. I wouldn't exactly call the Sun-Blade-100's or Cray's you've used
common... In fact, I wouldn't call any mainframe or mini-frame (including
the ones I've used) common.

There are a lot of AMD64 and Intel 64 bit systems out here.

--
Ian Collins.
May 10 '06 #25
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
> <jo**@fcs.uga.edu> wrote in message
> news:11**********************@j33g2000cwa.googlegr oups.com...
>> Wow! This code is really awesome. I think this is the absolute best
>> way I have seen yet. I will have to study your code and break it down
>> to see how it works exaclty. Do you think that it could it be placed
>> within a #define ?
>>
>
> Yes. Just remember that it's only for 'unsigned long' 's which
> are 32-bits in length (i.e., rarely it's non-portable). You'd
> need a different version for other unsigned types: short, long
> long, etc. This method won't work with signed types.


I think what he means by "rarely it's non-portable" is "it's
non-portable". unsigned long isn't necessary 32 bits; I regularly use
systems on which it's 64 bits.


Just because you use a system regularly doesn't mean that it's a non-rare
system. I wouldn't exactly call the Sun-Blade-100's or Cray's you've used
common... In fact, I wouldn't call any mainframe or mini-frame (including
the ones I've used) common.


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*.

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?

All the world was not a VAX 25 years ago, and all the world is not a
32-bit x86 today.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
May 10 '06 #26

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
> <jo**@fcs.uga.edu> wrote in message
> news:11**********************@j33g2000cwa.googlegr oups.com...
>> Wow! This code is really awesome. I think this is the absolute best
>> way I have seen yet. I will have to study your code and break it down >> to see how it works exaclty. Do you think that it could it be placed >> within a #define ?
>>
>
> Yes. Just remember that it's only for 'unsigned long' 's which
> are 32-bits in length (i.e., rarely it's non-portable). You'd
> need a different version for other unsigned types: short, long
> long, etc. This method won't work with signed types.

I think what he means by "rarely it's non-portable" is "it's
non-portable". unsigned long isn't necessary 32 bits; I regularly use
systems on which it's 64 bits.
Just because you use a system regularly doesn't mean that it's a non-rare system. I wouldn't exactly call the Sun-Blade-100's or Cray's you've used common... In fact, I wouldn't call any mainframe or mini-frame (including the ones I've used) common.


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 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?
Rod Pemberton
May 11 '06 #27

Rod Pemberton wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
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.


I think this discussion is overblown. On the one hand, the O/P
specified 32 bit unsigned ints, and the solution you provided is OK
given that spec seems to me. It could add an assert(sizeof(int)==4 &&
CHAR_BITS==8) to make sure it isn't misused, but whatever.

On the other hand, the fact that many (at least non-embedded) machines
now use 32 bit ints/longs is not to me relevant. I'd be surprised if
most new desktop/server machines were not 64 bit in the relatively near
future. Writing code, wherever possible, to not care seems entirely
reasonable, unless one is trying to ensure future programming work
upgrading ones source code to be "64 bit safe". This is quite
different (IMHO) than the arguments about whether assuming NULL is all
bits 0 is OK based on the fact that it wasn't in some obsolescent
architectures. I'd go with the pedanticly correct side there too, but
I think it is relatively unlikely that someone will make a new machine
that doesn't treat all bits 0 as NULL. It is a certainty that many new
machines will be released where compilers use long ints that are 64
bits...

-David

May 11 '06 #28
On 2006-05-11, Rod Pemberton <do*********@bitfoad.cmm> wrote:
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.


On what 64-bit machines is unsigned long int a 32-bit type? That's more
rare than for it to be 64 bits.
May 11 '06 #29
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...

[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
May 11 '06 #30

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...

[...]
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
May 12 '06 #31
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> 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) ks***@mib.org <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.
May 12 '06 #32

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> 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
May 12 '06 #33
Rod Pemberton wrote:

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
> "Rod Pemberton" <do*********@bitfoad.cmm> 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
May 13 '06 #34
pete wrote:

Rod Pemberton wrote:

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
> "Keith Thompson" <ks***@mib.org> wrote in message
> news:ln************@nuthaus.mib.org...
>> "Rod Pemberton" <do*********@bitfoad.cmm> 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
May 13 '06 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
9
by: storyGerald | last post by:
I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; }
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
11
by: Jiang Nutao | last post by:
Hi, I simplify my problem like below To convert list aa = into How to do it fast? My real list is huge.
8
by: sexauthor | last post by:
I'm converting a VB6 application over that called a 3rd party DLL with specific data structures. The VB6 code defined custom types for those data structures (ie: one with the specific data types,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.