C Algorithm
Date: May 2012
Level: Intermediate


INTERVIEW QUESTIONS

Company: Cisco Systems
Question Title: C Algorithm
Language: C
QUESTION DETAILS:

What will be the output of the following C code?
#include <stdio.h>
main ()
{
 printf("%x", -1<<4);
}


Output will be: fffffff0
Explanation:
-1<<4 = -16
Since it's a negative (signed) number, it is an arithmetic shift. The MSB value remains and does not go away.
-16 in 2's compliment is represented as follows:
16 in binary:
00000000 00000000 00000000 00010000
Flip all the bits now:
11111111 11111111 11111111 11101111
Now add "1" to it.
11111111 11111111 11111111 11110000
which is nothing but fffffff0.
All Questions