Swap byte 2 and 4 from integer
- by czar x
I had this interview question -
Swap byte 2 and byte4 within an integer sequence.
Integer is a 4byte wide i.e. 32 bits
My approach was to use char *pointer and a temp char to swap the bytes.
For clarity i have broken the steps otherwise an character array can be considered.
unsigned char *b2, *b4, tmpc;
int n = 0xABCD;
b2 = &n; b2++;
b4 = &n; b4 +=3;
///swap the values;
tmpc = *b2;
*b2 = *b4;
*b4 = tmpc;
Any other methods?