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)
4 Answers
+ 1
(x & 0xffff0000)
| ((x << 8) & 0xff00)
| ((x >> 8) & 0xff)
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`.
0
I've tried this, but it is not correct answer '(
0
Thank u very muchđ¸đ¸