我有这个面试问题 -
交换整数序列中的字节
2和字节4.整数是4字节宽,即32位
我的方法是使用char *pointer和a temp char来交换字节.为清楚起见,我已经打破了步骤,否则可以考虑字符数组.
unsigned char *b2, *b4, tmpc;
int n = 0xABCD; ///expected output 0xADCB
b2 = &n; b2++;
b4 = &n; b4 +=3;
///swap the values;
tmpc = *b2;
*b2 = *b4;
*b4 = tmpc;
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?