Swap Odd and Even Bits
Write a C function to swap all odd and even bits in an unsigned integer. Bit positions 0, 2, 4, ... are considered even bits, and positions 1, 3, 5, ... are odd bits.
Ex input: 10101010 (in binary),
Expected O/P: 01010101.
It may look simple at first, well, like just outputting its complement, but is that right?
What if the input is 11010010?
How would you proceed with this question?
2 Replies
Complement works, right?
Ah, but you have to limit the length of the values to be complemented. (& 0xff, etc...)