I have an arbitrary 8-bit binary number e.g., 11101101
I have to swap all the pair of bits like:
Before swapping: 11-10-11-01
After swapping: 11-01-11-10
I was asked this in an interview !
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In pseudo-code:
It works by handling the low bits and high bits of each bit-pair separately and then combining the result:
x & 0b10101010extracts the high bit from each pair, and then>> 1shifts it to the low bit position.(x & 0b01010101) << 1extracts the low bit from each pair and shifts it to the high bit position.Since not all languages allow you to write binary literals directly, you could write them in for example hexadecimal: