Someone I know was recently asked this question in a coding interview. They were asked to write a function `reverseBytes`

below such that if you pass it `0xabcdef12`

it’d output `0x21fedcba`

.

They gave the following solution.

```
int reverseBytes (int x) {
int ans = 0;
ans = (x & 0xff000000 >> 24) | (x & 0x00ff0000 >> 8) | (x & 0x000000ff << 24) | (x & 0x0000ff00 << 8);
return res;
}
```

Are there any further ways to optimize it? One can of course not do the 1st and 3rd mask, but they’d still need to do at least 2 bitmask operations, 4 bitwise-shift, and 3 bitwise-or, is that correct?

According to them, interviewer was expecting further optimizations. I’m at loss to see that there could be further optimization possible. If so, what are they?

Source: Windows Questions C++