C Endian Conversion : bit by bit
Asked Answered
V

4

-1

I have a special unsigned long (32 bits) and I need to convert the endianness of it bit by bit - my long represents several things all smooshed together into one piece of binary.

How do I do it?

Vacillatory answered 23/10, 2009 at 15:57 Comment(8)
Clarify this: you want something that's ordered 9876543210 to become 0123456789, except for 32 bits instead of 10? I ask because that's a pretty unusual operation, and is not what is normally referred to as an 'endian conversion'.Volumeter
If it was smooshed together in a certain way on one machine then by converting the endianness you are just putting the bits together properly on the the current machine. What are we missing here?Dextrose
Endian conversions usually refer to byte orderings in memory (or over the wire protocols) and not to the individual bit orderings contained within the bytes. On some machines, 0x76543210 is stored in memory as 0x10, 0x32, 0x54, 0x76 whereas other machines would store it as 0x76,0x54,0x32,0x10. The normal solutions involve swapping bytes around, not reordering the individual bits in the bytes.Volumeter
OP: you got a wrong idea. please improve your design from zero.Mather
I have a binary field, and kinda like tcp/ip, i have different things contained inside just one binary field, for example, bits 0-5 might be a field and then bits 6-7 might be a field... etc etc... and so i am left with oddly-sized binary numbers (say, 5,6,7 bits instead of standard 4,8,16...) and thus i need to convert the endianness bit-by-bit.. For example, I have binary 000100, 6 bits representing the number 8, but it needs to be 001000 for my architecture to represent 8.Vacillatory
@rlb.usa, I think you misunderstand the endianess problem. If you are dealing with single bytes then you don't have to worry about endianess at all. I'm not sure what an "oddly size binary number" is. If you have a a byte that you are using as a bit mask but aren't using all 8 bits then just put 0s in the places you don't care about. If you have 32bits and you are move between architectures then just use the htonl and ntohl functions anohter user specified.Ashlan
@rlb.usa: if different endianess, you and your network peer will always get wrong packet even had called htonx()/ntohx(), because the packet is on bit basis, not bytes. the design that what you need to improve is you and the peer should have high level agreement that either LE or BE at both ends when interpretting the bits from the packet.Mather
2,500 views for my question with -1 rating.Vacillatory
G
3

Endianness is a word-level concept where the bytes are either stored most-significant byte first (big endian) or least-significant byte first (little endian). Data transferred over a network is typically big endian (so-called network byte order). Data stored in memory on a machine can be in either order, with little endian being the most common given the prevalence of the Intel x86 architecture. Even though most computer architectures are big endian, the x86 is so ubiquitous that you'll most often see little endian data in memory.

Anyhow, the point of all that is that endianness is a very specific concept that only applies at the byte level, not the bit level. If ntohs(), ntohl(), htons(), and htonl() don't do what you want then what you're dealing with isn't endianness per se.

If you need to reverse the individual bits of your unsigned long or do anything else complicated like that, please post more information about what exactly you need to do.

Gardiner answered 23/10, 2009 at 16:9 Comment(0)
W
3

Be careful to understand the meaning of 'endianness'. It refers to the order of bytes within the data, not bits within the byte. You may only need to use a function like htonl or ntohl to convert your d-word.

If you truly want to reverse the order of all bits in the 32b data type, you could write an iterative algorithm to mask and shift each bit into the appropriate reflected position.

Wasp answered 23/10, 2009 at 16:16 Comment(0)
L
1

A simple endianess conversion function for an unsigned long value could look like the following:

typedef union {
  unsigned long u32;
  unsigned char u8 [ 4 ];
} U32_U8;

unsigned long SwapEndian(unsigned long u)
{
   U32_U8 source;
   U32_U8 dest;

   source.u32 = u;
   dest.u8[0] = source.u8[3];
   dest.u8[1] = source.u8[2];
   dest.u8[2] = source.u8[1];
   dest.u8[3] = source.u8[0];

   return dest.u32;
}
Lucan answered 23/10, 2009 at 18:50 Comment(0)
T
0

To invert the bit order of an integer, you can shift out the bits in one direction and shift the bits to the destination in the opposite direction.

Titration answered 28/12, 2014 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.