swapping bytes in c

Sep 25, 2017 at 9:06pm
I am trying to swap bytes in c.
I am suppose to swap the nth byte and the mth byte. I am given examples such as 0x12345678, 1, 3) = 0x56341278. 1 meaning n and 3 meaning m. I am suppose to use these operators to find a formula to swap these ! ~ & ~ ^ | + << >> i am allowed to use max 25 operators. I know this is not actually coding but i don't know where else to ask... Would be great if anyone could help me out in finding this out.
Sep 25, 2017 at 11:36pm
Some examples:

((0x12345678 >> 16) & 0xFF) == 0x34
(0x00000000 | (0xFF << 8)) == 0x0000FF00
~0xFF55FFAA == 0x00AA0055
(0xFFFFFFFF & ~(0xFF << 16)) = 0xFF00FFFF

For more information, google "bitwise operators".
Last edited on Sep 25, 2017 at 11:37pm
Sep 26, 2017 at 1:57am
the intel cpu has a built in operation to do this for endian reversal purposes; I think the command is 'bswap'. If you want to do a lot of them fast. (yes, I know its not in the assignment).

you can also brute force cast into unsigned char *s and swap them with the standard swap operations..

int x;
unsigned char * cp = (unsigned char*)(&x);
unsigned char tmp = cp[0];
cp[0] = cp[4];
cp[4] = tmp;

you can also swap things with xor:
a^=b
b^=a
a^=b

or use std::swap on it. (C++).

Using the assignment parameters, I would say the cast combined with xor swap are the cleanest.
Last edited on Sep 26, 2017 at 2:16am
Sep 26, 2017 at 6:52pm
The behavior of your first snippet is undefined. There's no telling how many bits are in an unsigned char, nor in what order the bytes are in memory.
Sep 26, 2017 at 8:00pm
true. you do need to know what you are working with when doing bit level code. This was for standard 1 byte chars against 8+ byte ints. The order of the bytes in memory of the integer is irrelevant to swapping them around. It will matter when trying to USE the data after manipulations though.
Last edited on Sep 26, 2017 at 8:01pm
Sep 26, 2017 at 9:51pm
The endianness is relevant, because OP needs to swap the nth and mth bytes of the integer, not just reverse the bytes of the integer. The result of your snippet is undefined, but the result of this isn't:
1
2
3
4
5
std::uint32_t n = 0x12345678;
std::uint8_t a = (n >> 8) & 0xFF;
std::uint8_t b = (n >> 24) & 0xFF;
n &= 0x00FF00FF;
n |= (b << 24) | (a << 8);
If you want to do something equivalent with direct memory accesses you'll need to know the endianness of the underlying platform.
Last edited on Sep 26, 2017 at 9:52pm
Sep 27, 2017 at 3:59am
I see. You are saying the nth byte in terms of human readable most significant is left format. I was saying the nth byte is from memory location zero. That is why you think the endian matters, and I don't. Makes perfect sense.


Sep 27, 2017 at 5:19am
It's not the "human readable form", it's the actual value stored in the variable.

That aside, did you even read the OP? OP makes it perfectly clear what the problem statement requires.
f(0x12345678, 1, 3) = 0x56341278
I am suppose to use these operators ! ~ & ~ ^ | + << >>

The question has nothing to do with memory locations, or endianness. It's basically a math problem.
Last edited on Sep 27, 2017 at 1:56pm
Topic archived. No new replies allowed.