Byte Swapping Program help
Okay so this program was asked from me in an interview and in spite of answering all the previous questions I couldn't clear it because I could not write this program.
Question: Consider a 32-bit machine where integer variable is of 4 bytes
|b1|b2|b3|b4|
Write a C program to swap b1 and b2 and swap b3 and b4. b1,b2,b3,b4 are byte 1, byte 2 and so on.
I proposed a method to convert the integer in binary format and then divide the binary format in 4 arrays of size 8 each and then rearrange them so that the bytes are swapped. But it was a lengthy program and he told me to write this program using Union and I couldn't. So please help me with this.
Question: Consider a 32-bit machine where integer variable is of 4 bytes
|b1|b2|b3|b4|
Write a C program to swap b1 and b2 and swap b3 and b4. b1,b2,b3,b4 are byte 1, byte 2 and so on.
I proposed a method to convert the integer in binary format and then divide the binary format in 4 arrays of size 8 each and then rearrange them so that the bytes are swapped. But it was a lengthy program and he told me to write this program using Union and I couldn't. So please help me with this.
0