Interview Question

Embedded Software Engineer Interview

-New York, NY

Apple

reverse bits of an integer that is a power of 2 , keeping complexity in mind.

AnswerAdd Tags

Interview Answers

4 Answers

3

rev_n = n ^ (~(n*0));

Ethan Hunt on

2

int reverse(int n) { int msb=Math.log(n)/Math.log(2); return Math.pow(2, 32-msb); }

Anonymous on

1

if the bits that need to be reversed are just the ones to the right of the 1 so all you need to do is: return n -1; 1000 -> 0111 1 -> 0 10 -> 1 10000000 -> 1111111

Anonymous on

0

uint flipBits(uint input) { uint bits = sizeof(input) * 8; uint msb = log2(input); return 1 << (bits - msb); }

Justin O. on

Add Answers or Comments

To comment on this, Sign In or Sign Up.