Thursday, 26 May 2011

Programming questions in Technical round

1.Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.] 
if (x && !(x & (x-1)) == 0)

2.What are the different ways to say, the value of x can be either a 0 or a 1.
Apparently the if then else solution has a jump when written out in assembly.
if (x == 0)
y=0
else
y =x
There is a logical, arithmetic and a datastructure soln to the above problem.
3.I was given two lines of assembly code which found the absolute value of a number stored in two's complement form. I had to recognize what the code was doing.
Pretty simple if you know some assembly and some fundaes on number representation
4.Give a fast way to multiply a number by 7.
Multiply by 8 (left shift by 3 bits) and then subtract the number.
(x << 3) - x

No comments:

Post a Comment