By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Translate bit order

P: n/a
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

ReverseBitOrder(unsigned long)
{
unsigned long newlong = 0;

// for each bit position in the long
// zero out all other bit's positions
// shift it to 32 - (the bit's position from least significant)
// add it to newlong;

return newlong;
}

Hell, I am not even sure if that is right.

Mar 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.
I am not 100% sure if that is correct.
>
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:
If you are on Linux/Unix, you can use these:

NAME
htonl, htons, ntohl, ntohs - convert values between host and
network
byte order

SYNOPSIS
#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Then you can test how the bits are massaged. Then you can attempt to
write your own version for portability or look for one on the Windows
side.

HTH!
Mar 9 '07 #2

P: n/a
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.
That's not right. It's the bytes within a word that you need to reverse
(what a word is depends on the application, usually it's two or four bytes).

That's why it's called netword byte order, not network bit order.
>
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:
Well, I guess you're glad to learn that you don't need to.

john
Mar 9 '07 #3

P: n/a
On Mar 9, 4:25 pm, John Harrison <john_androni...@hotmail.comwrote:
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

That's not right. It's the bytes within a word that you need to reverse
(what a word is depends on the application, usually it's two or four bytes).

That's why it's called netword byte order, not network bit order.
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

Well, I guess you're glad to learn that you don't need to.

john
Doh, thats what I orignally thought until someone wrote it on paper
with the bits reversed instead of bytes. I was wondering why I didn't
have to convert chars but I had to convert ints...

Mar 9 '07 #4

P: n/a
On Mar 9, 4:06 pm, Piyo <cybermax...@yahoo.comwrote:
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

I am not 100% sure if that is correct.
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

If you are on Linux/Unix, you can use these:

NAME
htonl, htons, ntohl, ntohs - convert values between host and
network
byte order

SYNOPSIS
#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Then you can test how the bits are massaged. Then you can attempt to
write your own version for portability or look for one on the Windows
side.

HTH!
Yea, linux has a function for it and windows has a function for it if
using winsock. But I gotta write my own as I am working on a modular
layer that can easily be plugged into anything underneath. All kinds
of reinventions are coming up.

Mar 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.