We help IT Professionals succeed at work.

How do i swap the bits in a byte to change it from big endian to little endian.

bahnah
bahnah asked
on
1,626 Views
Last Modified: 2009-07-29
I need to take a 1 byte char and swap the bits to change it from little endian to big endian. Is this possible in c++, and if so how would i do this? Any suggestions would be greatly appreciated. Thanks.
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Well i'm reading this data in from a file, and the creator of the file told me I would need to do this.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Not for a single byte. If this is an embedded system that requires *nibble* swapping (the upper and lowe 4 bits), you could use

char nibswap ( char c) {

    char rc = (c & 0x0f) << 4  |  (c & 0xf0) >> 4;

    return rc;
}
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Ooopes, make that better read

typedef unsigned char BYTE;

BYTE nibswap ( BYTE c) {

   BYTE rc = (c & 0x0f) << 4  |  (c & 0xf0) >> 4;

   return rc;
}
Also the characters in a file you are using may be Unicode chars, so one char is made of 16 bits and btw they are stored in little endian form. To read from such a file, you may simple read every second char starting from the first. Or read every two characters say as 'c1' and 'c2', and somehow to represent the value cu ('short cu = c2 << 8 | c1')
Top Expert 2004

Commented:
Make sure that your underlying data type is "unsigned char", otherwise shifting to the right will mess up your data.

Commented:
It's quite rare to have to turn around the bits in one byte.  It's not a "endian" problem,as "endian" is usually meant.
Better check with the guy who told you this, get an EXACT description, complete with diagrams of what to swap and how, and WHY.

If the machine that wrote this data is "other-Endian", then you have to turn around the BYTES within EACH data item.  That means:

If you're reading 16-bit integers, you have to swap the two bytes.  Like bytes  1-2  become 2-1

If you're reading 32-bit integers, you have to swap the bytes like this: from 1-2-3-4 to 4-3-2-1
Likewise if you're reading 32-bit reals.

If you're reading any larger data item, like IEEE 80-bit reals, swap them end-for-end: 1-2-3-4-5 becomes 5-4-3-2-1


jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>Make sure that your underlying data type is "unsigned char"

Funny, I was under the impression that

typedef unsigned char BYTE;

would take care of that.

Commented:
Oh, and if you have to do a HUGE number of these swaps where the swap-time is significant, the x86 CPU's have a MMX instruction that does the re-endianing mighty quickly.  

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks grg99 - It turns out i was initially correct in doing an endian change on the bytes of the data - and it wasn't a bits swap.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.