Apple Interview Question

Exchange the odd and even bits of an integer

Interview Answers

Anonymous

Oct 24, 2016

You have to get the odd and the even bits (using the & operator with 0xaaaaaaaa and 0x55555555). Then just shift the even to the right (>>1) and the odd to the left (<<1). Finally, result = newodd | neweven

Anonymous

Oct 24, 2016

Uhmm... not signed and appeared as anonymous

Anonymous

Oct 24, 2016

This is really a softball question but it's also pretty surprising that so many people is unable to nail it down. (( n & 0x55555555) > 1) & 0x55555555)

Anonymous

Oct 24, 2016

Some formatting issue ((n & 0x55555555) > 1) & 0x55555555)