When I write this code in VS, it doesn’t work (“Cannot implicitly convert ‘int’ to ‘short’. An explicit conversion exists. Are you missing a cast?”):
short A = 5;
short B = 1 << A;
Yet this code is absolutely fine:
short A = 1 << 5;
I know I can make the error go away by casting the entire expression as a short, but can anyone tell me why this happens?
Because A is not a literal, the compiler doesn’t know that the result is representable as a
short. Therefore it needs an explicit cast. With the literal 5, the compiler sees that the result is 32, which can fit in ashort.