Translate bit order

B

brekehan

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

Piyo

brekehan said:
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!
 
J

John Harrison

brekehan said:
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
 
B

brekehan

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.




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

brekehan

I am not 100% sure if that is correct.




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top