Microsoft Interview Question
Software Engineer in Testsx>>1 => x/2 only when x is power of 2.
Example:
if x=3(in decimal)=0011(in binary)
then x>>1 = 0001(in binary) =1(decimal)
do u think 3.5x=3.5*3 is same as (3>>1)+3+(3<<1)
kausik is right. x>>1 only meaning frul when x is power of 2.
Plus!! I believe this question was asked deliberately vague. I don't think this question is to ask multiplying w/o using any arithmetic, but to ask how you would handle integer type to correctly represent x3.5 situation. rounding to the integer is not acceptable..!
I guess the 1st solution is the best becoz it shifts only one digit left => least prone to overflow...while doing num<<2 shifts the digits 2 bits...which unnecessarily increases the probability of overflow
- Nitesh October 02, 2008