0

I need swap bytes, for example(0xAABBCCDD to 0xAABBDDCC). I wrote code below..

#include <stdio.h> int swapBytes(int x) { int res; res = ((x<<8)&0xff00)|((x>>8)&0x00ff); return res; } int main() { int x = 0xAABBCCDD; int d = swapBytes(x); printf("\n%x", d); return 0; } output: ddcc. How I can print like this "AABBDDCC"?? Thank u)

13th May 2020, 7:34 AM
Jai
Jai - avatar
4 ответов
+ 1
(x & 0xffff0000) | ((x << 8) & 0xff00) | ((x >> 8) & 0xff)
14th May 2020, 6:30 PM
Schindlabua
Schindlabua - avatar
0
(...)&0xff and (...)&0xff00 give you one byte each right? So your `res` only consists of two bytes. If you want to add the top two bytes to your number, you need another `x & 0xffff0000`.
13th May 2020, 8:10 AM
Schindlabua
Schindlabua - avatar
0
I've tried this, but it is not correct answer '(
13th May 2020, 8:17 AM
Jai
Jai - avatar
0
Thank u very much🌸🌸
14th May 2020, 6:33 PM
Jai
Jai - avatar