I was asked the output of the following code in my interview yesterday
#include <stdio.h>
int main(void){
printf ("%x" ,-1<<4);
}
I was given 2 minutes to tell the answer. I responded fffffff0. The result of the interview has not been declared yet. I want to know was my answer correct?
Technically left-shifting a negative integer invokes Undefined Behaviour. That means
-1<<4is UB. I dont know why they asked you this question. Probably they wanted to test your depth of knowledge of the C and C++ Standards.C99 [
6.5.7/4] saysC++03 makes it undefined behaviour by omitting the relevant text.